I have an XmlDocument in the format below. If I perform the following search
XmlNode title = xmlDoc.SelectNodes("//Book/Title[contains(., \"Title3\")]");
I will get back an XmlNode which is a title. How do I find out if that book falls under publications? I don't always want to assume that title.ParentNode.ParentNode.ParentNode exists. There should be an intuitive way to say:
if(title.hasAncestor("Publication") != null)
{
// do whatever
}
Any help will be greatly appreciated
<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>
You can do this in XPath by using the ancestor
axis:
//Book/Title[contains(., "Title3")][ancestor::Publications]