• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

VB.net XML editing

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

perfectturmoil

Member
Joined
Oct 23, 2004
Location
Hillbillyville
Hey yall.

I need to be able to find a line in an XML file and change its value to something else.

I found this section of code that ALMOST worked:
Code:
imports system.xml
...
Dim loXMLDoc As XmlDocument = New XmlDocument
Dim loNode As XmlNode
loXMLDoc.Load("C:\Path\To\Document.xml")
loNode = loXMLDoc.SelectSingleNode("//ParentNode/ChildNode1/ValueNode2")
loNode.InnerText = "UpdatedMoreData"
loXMLDoc.Save("C:\Path\To\Document.xml")
loNode = Nothing
loXMLDoc = Nothing

My problem is the XML files I'm trying to read from have their 'valuenodes' as <key name="something"> instead of just <key>

So I need to change the value in: //macros/macro/Block/Key-Value/Key name="PatternNames" but it says it has an invalid token.

If I just try to change .../key, it changes the first entry in that list. How can I reference the other nodes/values/ whatever?


Thanks for the help
 
Need to see your input (xml) file and a description of the nodes/line that your are trying to modify.
 
The SelectSingleNode method expects an XPath expression. You can read about them here.

Specifically, you need to qualify the node with the attribute. I believe you want something like this:

loNode = loXMLDoc.SelectSingleNode("//ParentNode/ChildNode1/key[@name='something']")
 
mccoyn said:
The SelectSingleNode method expects an XPath expression. You can read about them here.

Specifically, you need to qualify the node with the attribute. I believe you want something like this:

loNode = loXMLDoc.SelectSingleNode("//ParentNode/ChildNode1/key[@name='something']")

Yes, it does look like I want something like that.. I will try that when I get to work tomorrow.

Thanks for the help.
 
Back