2008-09-24 115 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