我有一个XmlDocument,格式如下。如果我执行以下搜索
XmlNode title = xmlDoc.SelectNodes("//Book/Title[contains(., \"Title3\")]");
我将找回一个标题的XmlNode。我怎么知道这本书是否属于出版物?我并不总是假设title.ParentNode.ParentNode.ParentNode存在。应该有一种直观的方式说:
if(title.hasAncestor("Publication") != null)
{
// do whatever
}
非常感谢任何帮助
<Publications>
<Novel>
<Book>
<Title>Title1</Title>
<Author>Author1</Author>
<Year>2000</Year>
</Book>
<Book>
<Title>Title2</Title>
<Author>Author2</Author>
<Year>2000</Year>
</Book>
</Novel>
<History>
<Book>
<Title>Title3</Title>
<Author>Author3</Author>
<Year>2000</Year>
</Book>
<Book>
<Title>Title4</Title>
<Author>Author4</Author>
<Year>2000</Year>
</Book>
</History>
</Publications>
<StudyGuides>
<Math>
<Book>
<Title>Title5</Title>
<Author>Author5</Author>
<Year>2000</Year>
</Book>
<Book>
<Title>Title6</Title>
<Author>Author6</Author>
<Year>2000</Year>
</Book>
</Math>
<Science>
<Book>
<Title>Title7</Title>
<Author>Author7</Author>
<Year>2000</Year>
</Book>
<Book>
<Title>Title8</Title>
<Author>Author8</Author>
<Year>2000</Year>
</Book>
</Science>
</StudyGuides>
答案 0 :(得分:0)
您可以使用ancestor
轴在XPath中执行此操作:
//Book/Title[contains(., "Title3")][ancestor::Publications]
答案 1 :(得分:0)
/Publications/*/Book/Title[contains(., 'Title3')]