Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Here is my xml file: configs.xml

<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?

Posted

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 Esposito's book, Applied XML Programming for Microsoft .NET. Review the XmlReadWriter project under the Chap04 folder.

"Never ascribe to malice that which can adequately be explained by incompetence." -- Napolean Bonaparte
Posted (edited)

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. :)

Edited by PlausiblyDamp

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...