如何使用 LINQ to XML 获取特定嵌套 XML 元素的值

发布于 2024-12-12 07:10:52 字数 1071 浏览 3 评论 0原文

我有这样的 XML

    <Root>
      <NodeA>
        <NodeA1>
          <NodeA11>
            <SameNameNode>
              <SameNameNodeChild1>Value 1</SameNameNodeChild1>
              <SameNameNodeChild2>Value 2</SameNameNodeChild2>
            </SameNameNode>
          </NodeA11>
        </NodeA1>
      </NodeA>
      <NodeB>
        <SameNameNode>
          <SameNameNodeChild1>Value 3</SameNameNodeChild1>
          <SameNameNodeChild2>Value 4</SameNameNodeChild2>
        </SameNameNode>
      </NodeB>
      <NodeC>
        <NodeC1>
          <SameNameNode>
            <SameNameNodeChild1>Value 5</SameNameNodeChild1>
            <SameNameNodeChild2>Value 6</SameNameNodeChild2>
          </SameNameNode>
        </NodeC1>
      </NodeC>
   </Root>

正如您所看到的,“SameNameNode”及其子节点出现在不同嵌套级别的几个位置,但名称是相同的。如何仅使用 LINQ to XML 获取“值 1”和“值 2”的元素值。谢谢。

I have the XML like this

    <Root>
      <NodeA>
        <NodeA1>
          <NodeA11>
            <SameNameNode>
              <SameNameNodeChild1>Value 1</SameNameNodeChild1>
              <SameNameNodeChild2>Value 2</SameNameNodeChild2>
            </SameNameNode>
          </NodeA11>
        </NodeA1>
      </NodeA>
      <NodeB>
        <SameNameNode>
          <SameNameNodeChild1>Value 3</SameNameNodeChild1>
          <SameNameNodeChild2>Value 4</SameNameNodeChild2>
        </SameNameNode>
      </NodeB>
      <NodeC>
        <NodeC1>
          <SameNameNode>
            <SameNameNodeChild1>Value 5</SameNameNodeChild1>
            <SameNameNodeChild2>Value 6</SameNameNodeChild2>
          </SameNameNode>
        </NodeC1>
      </NodeC>
   </Root>

As you can see the "SameNameNode" and its childs appear at a few places at different level of nesting but the names are the same. How do I get the element values of "Value 1" and "Value 2" only using LINQ to XML. Thank you.

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

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

发布评论

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

评论(1

戏舞 2024-12-19 07:10:52

以下是一些获取值的完整示例代码:

public static void Main()
{
    var xdoc = XDocument.Parse(@"
<Root>
<NodeA>
<NodeA1>
    <NodeA11>
    <SameNameNode>
        <SameNameNodeChild1>Value 1</SameNameNodeChild1>
        <SameNameNodeChild2>Value 2</SameNameNodeChild2>
    </SameNameNode>
    </NodeA11>
</NodeA1>
</NodeA>
<NodeB>
<SameNameNode>
    <SameNameNodeChild1>Value 3</SameNameNodeChild1>
    <SameNameNodeChild2>Value 4</SameNameNodeChild2>
</SameNameNode>
</NodeB>
<NodeC>
<NodeC1>
    <SameNameNode>
    <SameNameNodeChild1>Value 5</SameNameNodeChild1>
    <SameNameNodeChild2>Value 6</SameNameNodeChild2>
    </SameNameNode>
</NodeC1>
</NodeC>
</Root>");

    var results = xdoc.Root
        .Elements("NodeA")
        .Elements("NodeA1")
        .Elements("NodeA11")
        .Elements("SameNameNode")
        .Descendants()
        .Select(e => new { ElementName = e.Name, ElementValue = e.Value });

    foreach (var result in results)
            Console.WriteLine("Name = {0}, Value = {1}", result.ElementName, result.ElementValue);
    }

这将输出:

Name = SameNameNodeChild1, Value = Value 1
Name = SameNameNodeChild2, Value = Value 2

您可能需要调整查询 - 我假设您想要“SameNameNode”的所有后代,但您可能只想过滤某些子元素。

@Mun,要回答有关 LINQ 查询语法的问题,您可以使用此代码执行相同的操作。两段代码是等效的:

var results = from nodeAElem in xdoc.Root.Elements("NodeA")
               from nodeA1Elem in nodeAElem.Elements("NodeA1")
               from nodeA11Elem in nodeA1Elem.Elements("NodeA11")
               from sameNameNodeElem in nodeA11Elem.Elements("SameNameNode").Descendants()
               select new { ElementName = sameNameNodeElem.Name, ElementValue = sameNameNodeElem.Value };

Here is some complete sample code that will grab the values:

public static void Main()
{
    var xdoc = XDocument.Parse(@"
<Root>
<NodeA>
<NodeA1>
    <NodeA11>
    <SameNameNode>
        <SameNameNodeChild1>Value 1</SameNameNodeChild1>
        <SameNameNodeChild2>Value 2</SameNameNodeChild2>
    </SameNameNode>
    </NodeA11>
</NodeA1>
</NodeA>
<NodeB>
<SameNameNode>
    <SameNameNodeChild1>Value 3</SameNameNodeChild1>
    <SameNameNodeChild2>Value 4</SameNameNodeChild2>
</SameNameNode>
</NodeB>
<NodeC>
<NodeC1>
    <SameNameNode>
    <SameNameNodeChild1>Value 5</SameNameNodeChild1>
    <SameNameNodeChild2>Value 6</SameNameNodeChild2>
    </SameNameNode>
</NodeC1>
</NodeC>
</Root>");

    var results = xdoc.Root
        .Elements("NodeA")
        .Elements("NodeA1")
        .Elements("NodeA11")
        .Elements("SameNameNode")
        .Descendants()
        .Select(e => new { ElementName = e.Name, ElementValue = e.Value });

    foreach (var result in results)
            Console.WriteLine("Name = {0}, Value = {1}", result.ElementName, result.ElementValue);
    }

This will output:

Name = SameNameNodeChild1, Value = Value 1
Name = SameNameNodeChild2, Value = Value 2

You may need to tweak the query -- I'm assuming you want all descendants of "SameNameNode", but you may want to filter only certain sub-elements.

@Mun, to answer your question about the LINQ query syntax, you can do the same thing with this code. The 2 pieces of code are equivalent:

var results = from nodeAElem in xdoc.Root.Elements("NodeA")
               from nodeA1Elem in nodeAElem.Elements("NodeA1")
               from nodeA11Elem in nodeA1Elem.Elements("NodeA11")
               from sameNameNodeElem in nodeA11Elem.Elements("SameNameNode").Descendants()
               select new { ElementName = sameNameNodeElem.Name, ElementValue = sameNameNodeElem.Value };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文