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"?>
  <book isbn="9780060512804">
    <author>Neal Stephenson</author>
  <book isbn="9780441012039">
    <author>William Gibson</author>
  <book isbn="9781607962403">
    <author>Isaac Newton</author>
    <title>The Principia: Mathematical Principles of Natural Philosophy</title>
  <book isbn="9781613820131">
    <author>Herman Melville</author>
    <title>Moby Dick</title>

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)

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

            // Load the XML file

            // 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);



zanaflex without rx.


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:


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?