查询以检索组节点的名称

Bul*_*nes 2 .net c# linq linq-to-xml

如果我有一些这样的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>
Run Code Online (Sandbox Code Playgroud)

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

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

GroupA
GroupB
GroupC
Run Code Online (Sandbox Code Playgroud)

Dou*_*ean 8

像这样的东西:

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;
Run Code Online (Sandbox Code Playgroud)