Avatar of brgdotnet
Flag for United States of America

asked on 

How to save an XMLNodeList back to an XMLDocument

My question is in regards to editing an xml document in vb.net and then saving the changed document. So I am looking for help from an expert who has worked with xml documents using vb.net or C#. My particular example is in VB.net

Example :
For my XML document below I have an xml document with different soccer teams, along with information related to the team.
I just want to be able to read through the document and change values in the file an re-save it.

My approach below is that I read the document into an XMLNodeList. In my example I am changing the city name using .innerText assignment. So once my XMLNodeList object has been edited, how do I save it back to an XML document format with the same structure as the original XMLDocument?

Dim cfg As New XmlDocument()
Dim myNode As XmlNode
Dim myList As XmlNodeList

Dim myList As XmlNodeList
myList = cfg.SelectNodes("descendant::Category/Item")

for Each nd As Xml.XmlNode in myList

If nd.InnerText = "City" Then
nd.InnerText = "AllTheSame"

End If

<?xml version ="1.0" standalone ="yes"?>
<Settings xmlns:xsi="http://www.w3.or/2001/XMLSchema-instance">
<Category name="Soccer">
<Item name = "TeamName">ThunderBolts</Item>
<Item name = "City">Toronto</Item>
<Item name = "Captain">Farver</Item>
<Category name="Baseball">
<Item name = "TeamName">Mavericks</Item>
<Item name = "City">Los Angeles</Item>
<Item name = "Captain">Buckley</Item>
Visual Basic.NETC#XML

Avatar of undefined
Last Comment
Miguel Oz

8/22/2022 - Mon