How to get the value of a specific nested XML element using LINQ to XML

安稳与你 提交于 2019-12-12 01:06:18

问题


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.


回答1:


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 };


来源:https://stackoverflow.com/questions/7939168/how-to-get-the-value-of-a-specific-nested-xml-element-using-linq-to-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!