troubleshooting Question

How to save an XMLNodeList back to an XMLDocument

Avatar of brgdotnet
brgdotnetFlag for United States of America asked on
XMLC#Visual Basic.NET
1 Comment1 Solution118 ViewsLast Modified:
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

cf.Load("Teams.xml")
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
 
Next



<?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>
<Category name="Baseball">
<Item name = "TeamName">Mavericks</Item>
<Item name = "City">Los Angeles</Item>
<Item name = "Captain">Buckley</Item>
</Category>
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 1 Comment.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 1 Comment.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros