0

我有一个格式如下的 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>
4

2 回答 2

0

您可以使用ancestor轴在 XPath 中执行此操作:

//Book/Title[contains(., "Title3")][ancestor::Publications]
于 2011-11-10T08:21:04.100 回答
0
/Publications/*/Book/Title[contains(., 'Title3')]
于 2011-11-10T09:32:15.870 回答