如何使用 Linq to XML 查询搜索节点?

发布于 2024-08-07 12:52:01 字数 2117 浏览 2 评论 0原文

在 xml 文档下面

    <Root>
        <Global>
        </Global>
        <local>
            <section name="A">
                <subsection name="A">
                    <innersection name="A">
                        <Property1>
                        </Property1>
                    </innersection>
                    <innersection name="B">
                        <Property1>
                        </Property1>
                    </innersection>
                </subsection>
                <subsection name="B">
                    <innersection name="A">
                        <Property1>
                        </Property1>
                    </innersection>
                    <innersection name="B">
                        <Property1>
                        </Property1>
                    </innersection>
                </subsection>
            </section>
            <section name="B">
                <subsection name="A">
                    <innersection name="A">
                        <Property1>
                        </Property1>
                    </innersection>
                    <innersection name="B">
                        <Property1>
                        </Property1>
                    </innersection>
                </subsection>
                <subsection name="B">
                    <innersection name="A">
                        <Property1>
                        </Property1>
                    </innersection>
                    <innersection name="B">
                        <Property1>
                        </Property1>
                    </innersection>
                </subsection>
            </section>
        </local>
    </Root>

现在我想要使用 linq to xml 在一个查询中使用其节名称 =“B”、小节名称 =“B”和内部节名称 =“B”的 property1。

Below the xml doc

    <Root>
        <Global>
        </Global>
        <local>
            <section name="A">
                <subsection name="A">
                    <innersection name="A">
                        <Property1>
                        </Property1>
                    </innersection>
                    <innersection name="B">
                        <Property1>
                        </Property1>
                    </innersection>
                </subsection>
                <subsection name="B">
                    <innersection name="A">
                        <Property1>
                        </Property1>
                    </innersection>
                    <innersection name="B">
                        <Property1>
                        </Property1>
                    </innersection>
                </subsection>
            </section>
            <section name="B">
                <subsection name="A">
                    <innersection name="A">
                        <Property1>
                        </Property1>
                    </innersection>
                    <innersection name="B">
                        <Property1>
                        </Property1>
                    </innersection>
                </subsection>
                <subsection name="B">
                    <innersection name="A">
                        <Property1>
                        </Property1>
                    </innersection>
                    <innersection name="B">
                        <Property1>
                        </Property1>
                    </innersection>
                </subsection>
            </section>
        </local>
    </Root>

Now i want the property1 whose section name="B" and subsection name="B" and innersection name="B" in one single query using linq to xml.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

离鸿 2024-08-14 12:52:01

这是我的看法,替代 Jon 的看法,假设 Property1 在内部部分中仅出现一次,并且您只需要那个:

var Property1 = doc.Root.Elements("local").Elements("section")
    .Where(x => x.Attribute("name") == "B").Elements("subsection")
    .Where(x => x.Attribute("name") == "B").Elements("innersection")
    .Where(x => x.Attribute("name") == "B").Element("Property1");

Here is my take, alternative to Jon's, assuming Property1 occurs only once inside an innersection and you need only that one:

var Property1 = doc.Root.Elements("local").Elements("section")
    .Where(x => x.Attribute("name") == "B").Elements("subsection")
    .Where(x => x.Attribute("name") == "B").Elements("innersection")
    .Where(x => x.Attribute("name") == "B").Element("Property1");
煮酒 2024-08-14 12:52:01

编辑:删除了 LINQ to XML“正常”样式的查询,因为 ssg 的查询更好。不过我想保留 XPath 版本。它未经测试,但我认为它应该有效......

var properties = doc.XPathSelectElements(
 "//section[@name='B']/subsection[@name='B']/innersection[@name='B']/property1");

EDIT: Removed LINQ to XML "normal" style of query as ssg's is better. I wanted to leave the XPath version though. It's untested, but I think it should work...

var properties = doc.XPathSelectElements(
 "//section[@name='B']/subsection[@name='B']/innersection[@name='B']/property1");
走走停停 2024-08-14 12:52:01

由于这个问题也被标记为 vb.net,因此这里相当于 ssg 在 vb.net 中的查询:

Dim Property1 = doc.<local>.<section>.Where(Function(x) x.@name = "B") _
                           .<subsection>.Where(Function(x) x.@name = "B") _
                           .<innersection>.Where(Function(x) x.@name = "B") _
                           .<Property1>

Since this question was also tagged vb.net, here is the equivalent of ssg's query in vb.net:

Dim Property1 = doc.<local>.<section>.Where(Function(x) x.@name = "B") _
                           .<subsection>.Where(Function(x) x.@name = "B") _
                           .<innersection>.Where(Function(x) x.@name = "B") _
                           .<Property1>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文