[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
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.
Re: Writing to xml on every loop
Hi Stan,
Thanks for the advice :) Explicit was already on, but i just turned on Strict :) lol, i always like to disect code and see how it works heh, i was quite proficient in PHP, so i picked up the basics pretty quick, now just to master the rest.
The fix i did for the above was to take the writer out the function and put it above the for loop which fed into the function so as the values were returned i looped and wrote them to the xml file :)
cheers mate
Graham