C# Reading XML Files
Posted: 19/12/2011 Filed under: Programming | Tags: C#, XML 2 Comments »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 -->
3: <Books>
4: <Book>
5: <Title>Master ASP.NET Development</Title>
6: <Publisher>MS Press</Publisher>
7: <Author>Scott Guthrie</Author>
8: </Book>
9: <Book>
10: <Title>Deep Dive Web Programming</Title>
11: <Publisher>MS Press</Publisher>
12: <Author>Scott Hanselman</Author>
13: </Book>
14: <Book>
15: <Title>ASP.NET Good Sense</Title>
16: <Publisher>MS Press</Publisher>
17: <Author>Jon Galloway</Author>
18: </Book>
19: </Books>
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");
4:
5: while (xml.Read()) // Ensure there is content.
6: {
7: if (xml.NodeType.Equals(XmlNodeType.Element)) // Find all nodes that are elements.
8: {
9: switch (xml.Name) // Evaluates each element by its name.
10: {
11: case "Title":
12: Console.WriteLine();
13: Console.WriteLine(xml.ReadElementString("Title")); // Reads the value of the element.
14: break;
15: case "Publisher":
16: Console.WriteLine(xml.ReadElementString("Publisher")); // Reads the value of the element.
17: break;
18: case "Author":
19: Console.WriteLine(xml.ReadElementString("Author")); // Reads the value of the element.
20: break;
21: default:
22: break;
23: }
24: }
25: }
My output for the above would be as below:
Master ASP.NET Development
MS Press
Scott Guthrie
Deep Dive Web Programming
MS Press
Scott Hanselman
ASP.NET Good Sense
MS Press
Jon Galloway
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();
3: xml.Load(@"C:\Projects\ConsoleReadXML\ConsoleReadXML\Books.xml");
4:
5: // Retrieve the parent node of the document <Book>
6: XmlElement parent = xml.DocumentElement;
7: XmlNodeList children = parent.ChildNodes;
8:
9: for (int i = 0; i < children.Count; i++)
10: {
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;
16:
17: Console.WriteLine();
18: Console.WriteLine(title.InnerText);
19: Console.WriteLine(publisher.InnerText);
20: Console.WriteLine(author.InnerText);
21: }
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:
children[i].NextSibling.NextSibling.NextSibling.NextSibling.InnerText
You get the picture.
Happy coding.