Reading XML file in C# with XpathNavigator

后端 未结 2 487
死守一世寂寞
死守一世寂寞 2021-01-12 14:15

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

     

           


        
相关标签:
2条回答
  • 2021-01-12 14:59

    you can also use this "//title" instead of "/bookstore/book"

    0 讨论(0)
  • 2021-01-12 15:13

    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.

    0 讨论(0)
提交回复
热议问题