Parsing XML in C#

This example parses an XML file with a collection of books into a DOM tree using a console application.

XML file with a collection of books

<?xml version="1.0" encoding="utf-8"?>
<books>
  <book isbn="9780060512804">
    <author>Neal Stephenson</author>
    <title>Cryptonomicon</title>
  </book>
  <book isbn="9780441012039">
    <author>William Gibson</author>
    <title>Neuromancer</title>
  </book>
  <book isbn="9781607962403">
    <author>Isaac Newton</author>
    <title>The Principia: Mathematical Principles of Natural Philosophy</title>
  </book>
  <book isbn="9781613820131">
    <author>Herman Melville</author>
    <title>Moby Dick</title>
  </book>
</books>

Console application

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace ParseXML
{
    class Program
    {
        static void Main(string[] args)
        {
            ParseXML(@"..\..\XML\books.xml");
            Console.ReadLine();
        }

        static void ParseXML(string fileName)
        {
            XmlDocument xmlDocument = new XmlDocument();

            // Load the XML file
            xmlDocument.Load(fileName);

            // Get all book nodes
            XmlNodeList bookNodes = xmlDocument.GetElementsByTagName("book");

            // Iterate through all books
            foreach (XmlNode bookNode in bookNodes)
            {
                // Use indexers to get the isbn, author and title
                XmlAttribute isbnAttribute = bookNode.Attributes["isbn"];
                XmlNode authorNode = bookNode["author"];
                XmlNode titleNode = bookNode["title"];

                // Print the book information to the console
                Console.WriteLine("ISBN: " + isbnAttribute.Value);
                Console.WriteLine("Author: " + authorNode.InnerText);
                Console.WriteLine("Title: " + titleNode.InnerText);
                Console.WriteLine();
            }

        }

    }
}

Output:

ISBN: 9780060512804
Author: Neal Stephenson
Title: Cryptonomicon

ISBN: 9780441012039
Author: William Gibson
Title: Neuromancer

ISBN: 9781607962403
Author: Isaac Newton
Title: The Principia: Mathematical Principles of Natural Philosophy

ISBN: 9781613820131
Author: Herman Melville
Title: Moby Dick

You can download the VS 2010 project from here.

This entry was posted in C# and tagged , , , , , , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

One Comment

  1. Posted August 18, 2014 at 8:02 am | Permalink

    The XmlReader is a faster and less memory consuming alternative. It lets you run through the XML string one element at a time, while allowing you to look at the value, and then moves on to the next XML element.

    example source:

    http://net-informations.com/q/faq/xmlreader.html

    Steve

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Why ask?