Posted: 19/12/2011 Filed under: Programming | Tags: C#, XML
This post is written with the intention of showing you very quickly how to read an XML document through code.
Listed below is my XML file that I will be working on, filled with fictional data. It’s essentially a listing of books, with their Title, Publisher and Author.
1: <?xml version="1.0" encoding="utf-8" ?>
2: <!-- Listing of all Books -->
5: <Title>Master ASP.NET Development</Title>
6: <Publisher>MS Press</Publisher>
7: <Author>Scott Guthrie</Author>
10: <Title>Deep Dive Web Programming</Title>
11: <Publisher>MS Press</Publisher>
12: <Author>Scott Hanselman</Author>
15: <Title>ASP.NET Good Sense</Title>
16: <Publisher>MS Press</Publisher>
17: <Author>Jon Galloway</Author>
The simplest approach to reading this file would be to use the XmlTextReader class (part of the System.Xml namespace). The code snippet below shows you how to put this class into action.
1: // Initialise object by passing in a known path to XML file.
2: // This can also be an URL.
3: XmlTextReader xml = new XmlTextReader(@"C:\Projects\ConsoleReadXML\ConsoleReadXML\Books.xml");
5: while (xml.Read()) // Ensure there is content.
7: if (xml.NodeType.Equals(XmlNodeType.Element)) // Find all nodes that are elements.
9: switch (xml.Name) // Evaluates each element by its name.
11: case "Title":
13: Console.WriteLine(xml.ReadElementString("Title")); // Reads the value of the element.
15: case "Publisher":
16: Console.WriteLine(xml.ReadElementString("Publisher")); // Reads the value of the element.
18: case "Author":
19: Console.WriteLine(xml.ReadElementString("Author")); // Reads the value of the element.
My output for the above would be as below:
Master ASP.NET Development
Deep Dive Web Programming
ASP.NET Good Sense
Press any key to continue . . .
Note, in the snippet above, I’ve used a pre-defined path to my XML document. The use the ReadElementString() method to get the value of the elements.
Although this approach is quick, there is a small annoyance with it. For example, what if I have a I would like to enumerate through all elements that belong to each book element? You could still achieve this but it’s gets messy, especially when you need to used the data from each book element.
The code snippet below shows you how to achieve this by using the XmlDocument class instead.
1: // Initialise and load Xml document from pre-defined path.
2: XmlDocument xml = new XmlDocument();
5: // Retrieve the parent node of the document <Book>
6: XmlElement parent = xml.DocumentElement;
7: XmlNodeList children = parent.ChildNodes;
9: for (int i = 0; i < children.Count; i++)
11: // Retrieve child nodes under parent
12: // as defined in xml document.
13: XmlNode title = children[i].FirstChild;
14: XmlNode publisher = title.NextSibling;
15: XmlNode author = publisher.NextSibling;
The output for the above will remain the same. Note however, I am looking for all elements within a parent element, in this case <Book> then listing them out. The reason I’ve used the XmlNode object is simply to keep things clean.
Why didn’t I just used NextSibling? Things would have got quite silly if I have more child elements involved. Imagine having something like this:
You get the picture.