<?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>
我想选择不同级别的所有书籍,然后显示每本书的信息(作者、标题和价格)。目前,代码还将显示第一本书的其他书籍。仅显示所需信息的最佳方式是什么。我需要使用 XPath。
xPathDoc = new XPathDocument(filePath);
xPathNavigator = xPathDoc.CreateNavigator();
XPathNodeIterator xPathIterator = xPathNavigator.Select("/bookstore//book");
foreach (XPathNavigator navigator in xPathIterator)
{
XPathNavigator clone = navigator.Clone();
clone.MoveToFirstChild();
Console.WriteLine(clone.Name + " : " + clone.Value);
while (clone.MoveToNext())
{
Console.Write(clone.Name + " : " + clone.Value + " | ");
}
}