Writing/Editing XML

Kris Jacyna

Member
Joined
Feb 12, 2007
Messages
20
Programming Experience
Beginner
I am new to XML and I have a quick question.

I know how to read and load an xml document but I want to know how to edit it and save it. For example, I have a windows form which has a textbox and a button. I have an XML document containing the following:

VB.NET:
[COLOR=#0000ff]<?xml version="1.0" encoding="utf-8" ?>[/COLOR] 
[B][FONT=Courier New][COLOR=#ff0000] [/COLOR][/FONT][/B] [COLOR=#0000ff]<[/COLOR][COLOR=#990000]User[/COLOR][COLOR=#0000ff]>[/COLOR][B]kris[/B][COLOR=#0000ff]</[/COLOR][COLOR=#990000]User[/COLOR][COLOR=#0000ff]>[/COLOR]

On the button click I want what ever the user types into the textbox to be saved and replace "kris" in the XML doc.

If someone has some time could they explain how to do this.

Thanks,

Kris.
 
Look into the XmlDocument class and related classes in System.Xml namespace. Quick example that does what you asked:
VB.NET:
Dim filename As String = "user.xml"
Dim xdoc As New Xml.XmlDocument
xdoc.Load(filename)
xdoc.DocumentElement.SelectSingleNode("/User").InnerText = TextBox1.Text
xdoc.Save(filename)
 
Back
Top