2008-09-24 75 views
2

的名字如果我有一些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 

回答

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; 
+0

的localName ......就是这样!谢谢 :) – Bullines 2008-09-24 00:37:57