|
-
Aug 26th, 2009, 04:55 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Writing to xml on every loop
Hi Guys,
I am very close to finishing this function lol, what i am trying to do is a for loop which:
1) Grabs the value
2) saves the value in an .xml file for later use
Currently when i use a for loop to feed in my function i over write the hiddenFields.xml on every pass, instead of adding to it.
Calling function:
vb.net Code:
For X As Integer = 0 To postingHiddenFields
Dim hiddenFieldsReturned As String = functionDealWithHiddenFields(HTMLResponse, postingHiddenFields, X)
Next
Function:
vb.net Code:
Function functionDealWithHiddenFields(ByVal HTML As String, ByVal numHiddenFields As Integer, ByVal X As Integer)
Try
'For i As Integer = 0 To numHiddenFields
'// The html source
Dim sText = HTML
'// The string we are searching for
Dim sPattern As String = "(?<=<input type=""hidden"" class="").*?(?="".*?')"
'// This is the regular expression
Dim regex As New Regex(sPattern, RegexOptions.IgnoreCase Or RegexOptions.Singleline)
'// The matches that we return are put in the matchcollection
Dim regexMatches As MatchCollection = regex.Matches(sText)
'// Return
'Return regexMatches(0).ToString
MessageBox.Show(regexMatches(X).ToString)
'// File location
Dim fileLocation As String = "Debug/hiddenFields.xml"
'// The XmlTextWriter that will build the XML file
Dim myXmlWriter As New Xml.XmlTextWriter(fileLocation, System.Text.Encoding.UTF8)
'// Add a few extra options to the XML
With myXmlWriter
.Formatting = Formatting.Indented
.Indentation = 3
'Write an element (this one is the root)
myXmlWriter.WriteStartDocument()
myXmlWriter.WriteStartElement("hiddenFields")
'Write the title element.
myXmlWriter.WriteStartElement("fieldNumber" & X)
myXmlWriter.WriteString(regexMatches(X).ToString)
myXmlWriter.WriteEndElement()
'// Close up
myXmlWriter.Close()
End With
Catch ex As Exception
'Return False
Return False
End Try
End Function
So basically on every pass of the function i'm overwriting the values instead of adding to them, i'm not sure of the proper way to do this, any help would be appreciated.
thanks a lot guys
Graham
-
Aug 26th, 2009, 08:10 AM
#2
Re: Writing to xml on every loop
The way you're doing right now, every time you call the function functionDealWithHiddenFields to write a node, the xml file is overwritten, thus your xml file will contains only what you write in the function. You need to modify your function to write all the nodes by looping thu the regex.matches collection and do the writing on each iteration. Have the function return a List(Of string) that holds all the hiddenfield names (or whatever string you want to be returned for each node written).
And this is very important: turn both Option Explicit and Option Strict ON. You will have to fix many errors in your current code after turning these options on, but it will help you a better coder by forcing you to code it right.
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
- Abraham Lincoln -
-
Aug 26th, 2009, 08:30 AM
#3
Thread Starter
Hyperactive Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|