我将这个示例 XML 保存在 books.xml 中:
<?xml version="1.0" encoding="utf-8" ?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>
An in-depth look at creating applications
with XML.
</description>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>
A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen
of the world.
</description>
</book>
</catalog>
我创建了一个文档和导航器,如下所示:
var document = new XPathDocument(@"books.xml");
var navigator = document.CreateNavigator();
var books = navigator.Select("/catalog/book");
我正在尝试浏览书籍节点并解析节点上下文。我可以读取属性,但无法弄清楚如何读取节点的值:
while (books.MoveNext())
{
var location = books.Current;
var book = new Book();
book.Id = location.GetAttribute("id", "");
// this line throws an exception.
book.Title = (string)location.Evaluate("title/text()") ;
}
有人对我从文档中遗漏的内容有一些见解吗?
请我知道 XElement、XmlDocument 和 XmlTextReader 解析方法,但需要弄清楚 XPathNavigator 如何工作以进行性能比较。
TIA。