如何使用XPath根据内容选择XML节点?
如果我是有以下xml,我想选择包含Ritchie的< author> -node来获取作者的全名:
<books>
<book isbn='0131103628'>
<title>The C Programming Language</title>
<authors>
<author>Ritchie, Dennis M.</author>
<author>Kernighan, Brian W.</author>
</authors>
</book>
<book isbn='1590593898'>
<title>Joel on Software</title>
<authors>
<author>Spolsky, Joel</author>
</authors>
</book>
</books>
答案 0 :(得分:23)
/books/book/authors/author[contains(., 'Ritchie')]
或
//author[contains(., 'Ritchie')]
答案 1 :(得分:4)
XPath的目的是:
/books/book/authors/author[contains(., 'Ritchie')]
在C#中,以下代码将返回“Ritchie,Dennis M。”:
xmlDoc.SelectSingleNode("/books/book/authors/author[contains(., 'Ritchie')]").InnerText;
答案 2 :(得分:4)
//author[contains(text(), 'Ritchie')]