Home   Blog   Contact  |  Admin

Editing An XML File

Learning to edit XML files is the final step in becoming profficient enough to make use of XML files effectively and efficiently. Knowing how to use XML files can be a very helpful and powerful asset.

Lets get started, the following and assumes you have declared Imports System.Xml. We will be working with the file created in the Writing A New XML File tutorial called "newxml.xml".

newxml.xml <?xml version="1.0" encoding="utf-8"?> <!--Test comment--> <xmlroot> <firstchild> <title>New XML File</title> <desc>This is an example xml file</desc> </firstchild> <secondchild attr1="value1" attr2="value2"> <![CDATA[cdata sections will not be parsed, so what you see here will be displayed exactly]]> </secondchild> </xmlroot> First off we'll change the title element in the firstchild node.

Code Dim xml As New XmlDocument xml.Load(Server.MapPath("~/newxml.xml")) xml.DocumentElement.FirstChild.SelectSingleNode("title").InnerText = "Updated XML File" xml.Save(Server.MapPath("~/newxml.xml")) Result ... <xmlroot> <firstchild> <title>Updated XML File</title> ... Next lets do something a little more tricky, we'll remove the comment at the top of the file. Note that from now on i will ignore the xml.Load and xml.Save lines.

File <?xml version="1.0" encoding="utf-8"?> <!--Test comment--> <xmlroot> ... Code xml.RemoveChild(xml.DocumentElement.PreviousSibling) Remove and Insert functions can be called from any node, and must be called from the node on which the function will operate. For example, to remove the "desc" element in "firstchild" I must call RemoveChild from the "firstchild" node.

File <xmlroot> <firstchild> <title>New XML File</title> <desc>This is an example xml file</desc> Code xml.DocumentElement.FirstChild.RemoveChild(xml.DocumentElement. FirstChild.SelectSingleNode("desc")) Now lets convert the first attribute in "secondchild" to a new element in "firstchild". We will then remove all the remaining attributes from "secondchild". A few things to notice, when creating a new element(or any new part for that matter) we use the XmlDocument variable of the document that the new element will be added. Another note, after the secondChild variable is assigned to a part of the xml file, any operations on secondChild will be effective in the xml file it was assigned to.

File <xmlroot> ... <secondchild attr1="value1" attr2="value2"> Code Dim newElement As XmlNode Dim secondChild As XmlNode secondChild = xml.DocumentElement.ChildNodes(1) newElement = xml.CreateElement(secondChild.Attributes(0).Name) newElement.InnerText = secondChild.Attributes(0).Value xml.DocumentElement.FirstChild.AppendChild(newElement) secondChild.Attributes.RemoveAll() Result <xmlroot> <firstchild> ... <attr1>value1</attr1> </firstchild> <secondchild> ... We've now covered most of the editing scenarios you will most likely encounter, but I will demonstrate one final method for moving child nodes. This example will swap a child of a given index with the child below it.

Code Dim index As Integer = 0 'The index of the child to be moved Dim tempNode As XmlNode tempNode = xml.DocumentElement.ChildNodes(index).Clone() xml.DocumentElement.RemoveChild(xml.DocumentElement.ChildNodes(index)) xml.DocumentElement.InsertAfter(tempNode, xml.DocumentElement.ChildNodes(index)) Result <xmlroot> <secondchild> ... <firstchild> ... That completes the XML Tutorial series, I hope this has provided you with an improved understanding of XML files in Asp.NET.