Results 1 to 3 of 3

Thread: [RESOLVED] Writing to xml on every loop

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2009
    Location
    Scotland
    Posts
    417

    Resolved [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:
    1. For X As Integer = 0 To postingHiddenFields
    2.                 Dim hiddenFieldsReturned As String = functionDealWithHiddenFields(HTMLResponse, postingHiddenFields, X)
    3.             Next

    Function:

    vb.net Code:
    1. Function functionDealWithHiddenFields(ByVal HTML As String, ByVal numHiddenFields As Integer, ByVal X As Integer)
    2.  
    3.         Try
    4.  
    5.             'For i As Integer = 0 To numHiddenFields
    6.             '// The html source
    7.             Dim sText = HTML
    8.  
    9.             '// The string we are searching for
    10.             Dim sPattern As String = "(?<=<input type=""hidden"" class="").*?(?="".*?')"
    11.  
    12.             '// This is the regular expression
    13.             Dim regex As New Regex(sPattern, RegexOptions.IgnoreCase Or RegexOptions.Singleline)
    14.  
    15.             '// The matches that we return are put in the matchcollection
    16.             Dim regexMatches As MatchCollection = regex.Matches(sText)
    17.  
    18.             '// Return
    19.             'Return regexMatches(0).ToString
    20.             MessageBox.Show(regexMatches(X).ToString)
    21.  
    22.  
    23.             '// File location
    24.             Dim fileLocation As String = "Debug/hiddenFields.xml"
    25.  
    26.             '// The XmlTextWriter that will build the XML file
    27.             Dim myXmlWriter As New Xml.XmlTextWriter(fileLocation, System.Text.Encoding.UTF8)
    28.  
    29.             '// Add a few extra options to the XML
    30.             With myXmlWriter
    31.  
    32.                 .Formatting = Formatting.Indented
    33.                 .Indentation = 3
    34.  
    35.                 'Write an element (this one is the root)
    36.                 myXmlWriter.WriteStartDocument()
    37.                 myXmlWriter.WriteStartElement("hiddenFields")
    38.  
    39.                 'Write the title element.
    40.                 myXmlWriter.WriteStartElement("fieldNumber" & X)
    41.                 myXmlWriter.WriteString(regexMatches(X).ToString)
    42.                 myXmlWriter.WriteEndElement()
    43.  
    44.                 '// Close up
    45.                 myXmlWriter.Close()
    46.  
    47.             End With
    48.  
    49.         Catch ex As Exception
    50.  
    51.             'Return False
    52.  
    53.             Return False
    54.  
    55.         End Try
    56.  
    57.     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

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    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 -

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2009
    Location
    Scotland
    Posts
    417

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width