PDA

View Full Version : Update XML textNode ASP.Net/VB.Net



lmod
08-22-2005, 09:42 PM
Does anyone know a way and/or a tutorial that shows how to update a XML textNode value?

ex:

XMLDoc
<ParentNode>
<ChildNode1>
<ValueNode1>SomeData</ValueNode1>
<ValueNode2>SomeMoreData</ValueNode2>
<ValueNode3>EvenMoreData</ValueNode3>
</ChildNode1>
</ParentNode>

I would like to know how to change,

<ValueNode2>SomeMoreData</ValueNode2>

To

<ValueNode2>UpdatedMoreData</ValueNode2>

language VB.Net/Asp.Net

boywonder
09-04-2005, 04:49 PM
Before anything else, you might want to bookmark this...

http://samples.gotdotnet.com/QuickStart/howto/default.aspx?url=/quickstart/howto/doc/xml/overviewofxml.aspx :)

Like most things in .NET, there are multiple ways of handling tasks, depending on your needs. Here's a simple snippet that opens an XML document, updates a node, and saves the document.


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

Mark Eastham
06-30-2006, 06:05 PM
I've been looking around for days to find a SIMPLE sample - anbd this is just it - I can use this to read a value then update it - SIMPLE!

CHEERS!:thumbsup:

dudeamisgriff
08-28-2008, 04:12 AM
I tried doing this with a function and failed... what am I doing wrong?

demtron
01-27-2009, 04:49 AM
Code that is very similar to this has worked for me in the past. Using SelectSingleNode is probably the most efficient way to do this.