c# – 获取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"}  

最佳答案

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