【问题标题】:Query to retrieve names of group nodes查询以检索组节点的名称
【发布时间】:2008-09-24 00:25:22
【问题描述】:

如果我将一些诸如此类的 XML 加载到 XDocument 对象中:

<Root>
    <GroupA>
        <Item attrib1="aaa" attrib2="000" />
    </GroupA>
    <GroupB>
        <Item attrib1="bbb" attrib2="111" />
        <Item attrib1="ccc" attrib2="222" />
        <Item attrib1="ddd" attrib2="333" />
    </GroupB>
    <GroupC>
        <Item attrib1="eee" attrib2="444" />
        <Item attrib1="fff" attrib2="555" />
    </GroupC>
</Root>

检索组节点名称的查询是什么样的?

例如,我想返回一个查询:

GroupA
GroupB
GroupC

【问题讨论】:

    标签: c# .net linq linq-to-xml


    【解决方案1】:

    类似这样的:

    XDocument doc; // populate somehow
    
    // this will give the names as XName
    var names = from child in doc.Root.Elements()
                select child.Name;
    
    // if you want just the local (no-namespaces) name as a string, use this
    var simpleNames = from child in doc.Root.Elements()
                      select child.Name.LocalName;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-15
      • 1970-01-01
      • 1970-01-01
      • 2021-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多