Home   Blog   Contact  |  Admin

Reading An XML File

First off, declare an XmlDocument and load your file. In this example we will be reading an xml file called "newxml.xml" which we created in the Writing A New XML File tutorial. Please Note, these code examples are assuming you have declared Imports System.Xml.

Dim xml As New XmlDocument xml.Load(Server.MapPath("~/newxml.xml")) So the easy bit's out of the way, now you need to know how to get inside the file. An XML file always has one and only one root node, called the DocumentNode in Asp.NET. A node can also be selected by name using SelectSingleNode(xpath As String), however for the root node using DocumentNode prevents misspelling the xpath (thats just the name of the node in this case).

When dealing with multiple child nodes, a single node can be selected using its index which starts at 0 for the first/top node. Child nodes of the same name can be selected together using SelectNodes(xpath As String) which returns the group of nodes of that name. Asp.NET also provides FirstChild and LastChild functions.

Using our example file, we will access the first child node in 3 different ways

File <?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> Code xml.DocumentElement.FirstChild xml.DocumentElement.SelectSingleNode("firstchild") xml.DocumentElement.ChildNodes(0) Now lets do something practical, we'll grab the title of this xml file, knowing that the first child node will contain an element called "title" holding the info we want. We will use the InnerText property to get the text inside the "title" element

File ... <xmlroot> <firstchild> <title>New XML File</title> <desc>This is an example xml file</desc> ... Code xml.DocumentElement.FirstChild.SelectSingleNode("title").InnerText Next, we'll look at getting data out of attributes. In our example file, a child node called "secondchild" has 2 attributes, called "attr1" and "attr2". Using the Attributes property, an attribute can be found using its name(usually most useful) or index. In this example we'll get the value of "attr2".

File <xmlroot> ... <secondchild attr1="value1" attr2="value2"> ... Code xml.DocumentElement.LastChild.Attributes("attr2").InnerText When finding the value of an attribute, there doesn't seem to be any difference between using InnerText or Value, so either should work in most cases.

Now that you know the basics of accessing different parts of the xml file, lets move on to a technique you might find useful when working with an xml file. This example is most suitable for working with a collection of child nodes all of the same format. Say you need to look through each child node, maybe to read a bit of information from each node, or to find one node in particular. Using the For Each loop is my prefered method.

Dim xml As XmlDocument xml.Load( ... ) For Each child As XmlNode In xml.DocumentElement.ChildNodes 'Read its first attribute 'child.Attributes(0).InnerText Next Hopefuly this gave you some reasonable understanding of how to make your way through an xml file in Asp.NET.

Next: Editing An XML File