Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading XML file in C# with XpathNavigator

Tags:

c#

xml

I am trying to read the book.xml file provided as an example on the MSDN website.

    <?xml version="1.0" encoding="utf-8" ?> 
<bookstore>
    <book genre="autobiography" publicationdate="1981-03-22" ISBN="1-861003-11-0">
        <title>The Autobiography of Benjamin Franklin</title>
        <author>
            <first-name>Benjamin</first-name>
            <last-name>Franklin</last-name>
        </author>
        <price>8.99</price>
    </book>
    <book genre="novel" publicationdate="1967-11-17" ISBN="0-201-63361-2">
        <title>The Confidence Man</title>
        <author>
            <first-name>Herman</first-name>
            <last-name>Melville</last-name>
        </author>
        <price>11.99</price>
    </book>
    <book genre="philosophy" publicationdate="1991-02-15" ISBN="1-861001-57-6">
        <title>The Gorgias</title>
        <author>
            <name>Plato</name>
        </author>
        <price>9.99</price>
    </book>
</bookstore>

I have the following code until now:

static void Main()
        {
            XmlDocument document = new XmlDocument();
            document.Load(@"c:\books.xml");

            XPathNavigator navigator = document.CreateNavigator();

            XPathNodeIterator nodes = navigator.Select("/bookstore/book");


            while (nodes.MoveNext())
            {
                Console.WriteLine(nodes.Current.HasAttributes);
            }


        }

It seems this code is reading everything, but from here on if I want to display, say, just the titles of all book etc., how do I access them?

like image 948
xbonez Avatar asked Sep 22 '10 12:09

xbonez


People also ask

How do I read XML files?

XML files can be opened in a browser like IE or Chrome, with any text editor like Notepad or MS-Word. Even Excel can be used to open XML files.

What is XML parser in C?

The Oracle XML parser for C reads an XML document and uses DOM or SAX APIs to provide programmatic access to its content and structure. You can use the parser in validating or nonvalidating mode. This chapter assumes that you are familiar with the following technologies: Document Object Model (DOM).

How read and write data from XML in C#?

The XmlReader, XmlWriter and their derived classes contains methods and properties to read and write XML documents. With the help of the XmlDocument and XmlDataDocument classes, you can read entire document. The Load and Save method of XmlDocument loads a reader or a file and saves document respectively.


1 Answers

You can iterate over the titles if you change the XPath expression to select all title nodes:

XPathDocument document = new XPathDocument(@"c:\tmp\smpl5.xml");
XPathNavigator navigator = document.CreateNavigator();

XPathNodeIterator nodes = navigator.Select("/bookstore/book/title");

foreach (XPathNavigator item in nodes)
{
    Console.WriteLine(item.Value);
}

Note that you don't need to create an XmlDocument if you don't plan to modify the document. Using an XPathDocument is usually more light-weight.

like image 140
Dirk Vollmar Avatar answered Sep 17 '22 20:09

Dirk Vollmar