根据同级节点选择 XML 节点 [英] Select a XML node based on a sibling node

查看:36
本文介绍了根据同级节点选择 XML 节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试选择一个 XML 节点,其中父节点的另一个子节点包含特定值.

I'm trying to select an XML node, where another child of the parent node contains a specific value.

XML 如下所示:

<?xml version="1.0" encoding="UTF-8" ?>
<AuthorIT>
    <Objects>
        <Media>don't care</Media>
        <Style>don't care</Style>
        <Book>don't care</Book>
        <Topic>don't care</Topic>
        <Topic>
            <Object>
                <Description>Performance Evidence</Description>
            </Object>
            <Text>This is what I want to select</Text>
        </Topic>
    </Objects>
</AuthorIT>

我在 C# 中使用 XPath.我目前的查询如下所示:(但显然不起作用)

I'm using XPath in C#. My query at the moment looks like this: (but doesn't work, obviously)

docNav = new XPathDocument(localFile);
nav = docNav.CreateNavigator();
xPath = "//Topic[Object/Description = 'Performance Evidence']/Text";
string value = nav.SelectSingleNode(xPath).Value;

如何从对象/描述值为性能证据"的主题中获取文本节点的内容?

How do I get the contents of the Text node, from the Topic that has an Object/Description value of "Performance Evidence"?

推荐答案

正如 Kirill Polishchuk 在评论中所说,我的 XPath 是正确的.

As Kirill Polishchuk said in a comment, my XPath was correct.

我在示例 XML 中遗漏的是解决方案的关键...命名空间!

What I left out of the example XML was the key to the solution... Namespace!

我在另一个问题上找到了答案:默认使用 XpathC#中的命名空间

I found my answer on this other question: Using Xpath With Default Namespace in C#

这篇关于根据同级节点选择 XML 节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆