【问题标题】:Fetching Xml Attributes获取 Xml 属性
【发布时间】:2011-10-23 16:14:44
【问题描述】:

我有一个如下的 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"}  

【问题讨论】:

  • 每个XML节点都有Attributes集合,所以遍历所有节点

标签: c# xml xml-parsing


【解决方案1】:
var allAttributes = XDocument.Parse(xmlInString)
                             .Descendants()
                             .Where(e => e.HasAttributes)
                             .SelectMany(e => e.Attributes())
                             .ToList();

【讨论】:

  • 非常感谢哈桑汗先生。
猜你喜欢
  • 2015-01-25
  • 1970-01-01
  • 2018-01-25
  • 2016-08-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-01
  • 2016-11-05
  • 2021-11-17
相关资源
最近更新 更多