VB.NET & XML - How to Search & Update


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: VB.NET & XML - How to Search & Update

  1. #1
    Join Date
    Mar 2005
    Posts
    14

    VB.NET & XML - How to Search & Update

    Hi,
    Im trying to search through an XML file. When it finds the record it needs it then updates a specific field in that record. I have made it find the specified record but do not know how to update it and write it back to file. I am new to XML coding. Any help would be most appreciated.

    Dim xpathDoc As XPathDocument
    Dim xmlNav As XPathNavigator
    Dim xmlNI As XPathNodeIterator
    Dim filePath As New OpenFileDialog

    filePath.ShowDialog()
    xpathDoc = New XPathDocument(filePath.FileName)
    xmlNav = xpathDoc.CreateNavigator()
    xmlNI = xmlNav.Select("/Message/Pupils/PupilCode")

    While (xmlNI.MoveNext())
    System.Console.WriteLine(xmlNI.Current.Name + " : " + xmlNI.Current.Value)

    If (xmlNI.Current.Value) = "A123981456002" Then

    'ONCE ITS FOUND THE RECORD - DISPLAY A MESSAGE
    'SELECT THE DIFFERENT FIELD WITHIN THE RECORD
    MsgBox("Found")
    xmlNav.Select("/Message/Pupils/PupilAge")
    'THIS IS WHERE IT NEEDS TO UPDATE IT. NO IDEA HOW TO DO THIS BIT
    End If
    End While


    This may be the wrong way to do it - I'm not sure. I just need it to search for a specific pupil ID and update their age with a specified value. After that it has to write the entire XML file to a new file.

    Many thanks.
    Chris
    Last edited by coleman01; 06-12-2005 at 11:37 AM.

  2. #2
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    Try this:
    Code:
    Dim xmlDoc As XmlDocument
    Dim xmlPupilNode As XmlNode
    Dim xmlAgeNode As XmlNode
    Dim FileName As String = "c:\temp\pupils.xml"
    
    xmlDoc = New XmlDocument
    xmlDoc.Load(FileName)
    xmlPupilNode = xmlDoc.SelectSingleNode("//Pupils[PupilCode = 'A123981456002']")
    
    If Not xmlPupilNode Is Nothing Then
        xmlAgeNode = xmlPupilNode.SelectSingleNode("PupilAge")
        If Not xmlAgeNode Is Nothing Then
            xmlAgeNode.InnerText = "17"
            xmlDoc.Save(FileName)
        End If
    End If
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center