read/write/modify xml

sde

Well-known member
Joined
Aug 28, 2003
Messages
160
Location
us.ca.fullerton
Here is my xml file: configs.xml
Code:
<configs>
  <config name="foo">
    <path>c:\mypath</path>
  </config>
  <config name="bar">
      <path>c:\anotherpath</path>
   </config>
</configs>

How can I get the info of 1 node by name, modify the path, and write it back to the xml file?
 
The short answer (the one I have time for now) is that you have to use XML DOM, which loads the entire document into memory, allows you to make edits, then writes it back to disk.

The other option is to download the samples from Dino Espositos book, Applied XML Programming for Microsoft .NET. Review the XmlReadWriter project under the Chap04 folder.
 
Code:
Private Sub frmReadWriteXML_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Dim xml_Document As New XmlDocument
    Dim xml_Node As XmlNode
    Dim strFilePath As String
    Dim strNewVal As String

    XML file path    
    strFilePath = "c:\temp\females.xml"

    New value for the Node.
    strNewVal = "Scarlett Johansson" She became 21 yesterday.

    Load document - place it into memorty.    
    xml_Document.Load(strFilePath)

    Select node.    
    xml_Node = xml_Document.SelectSingleNode("//Name")

    When node excist then write, otherwise do nothing.    
    If xml_Node Is Nothing Then
      Node not found.
    Else
      Node found - change content.
      xml_Node.InnerText = strNewVal

      Save document - write to harddisk.
      xml_Document.Save(strFilePath)
    End If

    Release resources.
    xml_Document = Nothing

  End Sub
End Class
I used this code for the same problems you asked for. Parts of this code comes from Visual Basic.NET Database programming by Rod Stephens. Apoligize for my not perfect English and good luck with your program. :)
 
Last edited by a moderator:
Back
Top