XPathNodeIterator access child nodes from iterated data
-
08-07-2021 - |
Domanda
<?xml version="1.0"?>
-<bookstore>
<book >
<title>aaaa</title>
-<author >
<first-name>firts</first-name>
<last-name>last</last-name>
</author>
<price>8.23</price>
<otherbooks>
<book >
<title>bbb</title>
<price>18.23</price>
</book>
<book >
<title>ccc</title>
<price>11.22</price>
</book>
</otherbooks>
</book>
</bookstore>
I have selected all books form xml file. How to select title, author( first and last name ) and price for each book with use of XPath?
xPathDoc = new XPathDocument(filePath);
xPathNavigator = xPathDoc.CreateNavigator();
XPathNodeIterator xPathIterator = xPathNavigator.Select("/bookstore//book");
foreach (XPathNavigator book in xPathIterator)
{
??
}
Soluzione
Use SelectSingleNode()
and Value
:
XPathDocument xPathDoc = new XPathDocument(filePath);
XPathNavigator xPathNavigator = xPathDoc.CreateNavigator();
XPathNodeIterator xPathIterator = xPathNavigator.Select("/bookstore//book");
foreach (XPathNavigator book in xPathIterator)
{
XPathNavigator nav = book.SelectSingleNode("title");
string title = nav==null ? string.Empty : nav.Value;
nav = book.SelectSingleNode("author/first-name");
string authorFirstName = nav==null ? string.Empty : nav.Value;
nav = book.SelectSingleNode("author/last-name");
string authorLastName = nav==null ? string.Empty : nav.Value;
nav = book.SelectSingleNode("price");
string price = nav==null ? string.Empty : nav.Value;;
Console.WriteLine("{0} {1} {2} {3}", title, authorFirstName, authorLastName, price);
}
Altri suggerimenti
You can use LINQ2XML
XElement doc=XElement.Load("yourXML.xml");//loads your xml
var bookList=doc.Descendants().Elements("book").Select(
x=>//your book node
new{
title=x.Element("title").Value,
author=new //accessing your author node
{
firstName=x.Element("author").Element("first-name").Value,
lastName=x.Element("author").Element("last-name").Value
},
price=x.Element("price").Value
}
);
bookList
now have all the elements you want
So, you can do this now
foreach(var book in bookList)
{
book.title;//contains title of the book
book.author.firstName;//contains firstname of that book's author
book.author.lastName;
}
I like the solution provided by Mimo but with a tiny change, creating an extension method to re-use part of the functionality:
public static class XPathNavigatorExtensions
{
public static string GetChildNodeValue(this XPathNavigator navigator, string nodePath)
{
XPathNavigator nav = navigator.SelectSingleNode(nodePath);
return nav == null ? string.Empty : nav.Value;
}
}
The resulting code will look like cleaner:
ICollection<Book> books = new List<Book>();
foreach (XPathNavigator node in iterator)
{
Book book = new Book() { Author = new Author() };
book.Title = node.GetChildNodeValue("title");
book.Author.FirstName = node.GetChildNodeValue("author/first-name");
book.Author.LastName = node.GetChildNodeValue("author/last-name");
book.Price = node.GetChildNodeValue("price");
books.Add(book);
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow