获取Xml属性

时间:2011-10-23 16:14:44

标签: c# xml xml-parsing

我有一个如下所示的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 个答案:

答案 0 :(得分:2)

var allAttributes = XDocument.Parse(xmlInString)
                             .Descendants()
                             .Where(e => e.HasAttributes)
                             .SelectMany(e => e.Attributes())
                             .ToList();