c# XmlNode有祖先类型
本文关键字:类型 祖先 XmlNode | 更新日期: 2023-09-27 18:13:56
我有一个格式如下的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>
在XPath中可以使用ancestor
轴:
//Book/Title[contains(., "Title3")][ancestor::Publications]
/Publications/*/Book/Title[contains(., 'Title3')]