2011-10-23 214 views
4

我有一個xml如下面:獲取XML屬性

<Phrase Entry="ID"> 
<Ans number="1"> 
    <Identification LastName="Bornery" Name="John" Age="23"/> 
    <Identification LastName="Grify" Name="Johnson" Age="29"/> 
    <Identification LastName="Alisen" Name="Julia" Age="38" City="NewYork" Job="Teacher"/> 
    <Identification LastName="Bornery" Name="John" Weight="85"/> 
</Ans> 
</Phrase> 

,我想列出的XML列表以及它們的屬性值,如以下列表:

MyList = {LastName="Bornery" , Name="John", Age="23" , LastName="Grify" , 
      Name="Johnson", Age="29", LastName="Alisen", 
      Name="Julia", Age="38", City="NewYork", Job="Teacher", 
      LastName="Bornery", Name="John", Weight="85"} 
+1

XML節點具有'Attributes'集合,因此遍歷所有節點 –

回答

2
var allAttributes = XDocument.Parse(xmlInString) 
          .Descendants() 
          .Where(e => e.HasAttributes) 
          .SelectMany(e => e.Attributes()) 
          .ToList(); 
每個
+0

非常感謝Hasan Khan先生。 – SMD

+0

@SMD不客氣 –