XDocument LINQ复杂语法

时间:2011-02-03 16:20:12

标签: linq-to-xml

我正在从XmlSerialization升级到Linq-to-xml,并且很难从此结构中获取对象。

我已尝试过帖子XmlSerializer - Deserialize different elements as collection of same element

中的建议

但是为了下面的结构而继续尝试获取null值。谢谢你的帮助。

  var hbs = from f in doc.Descendants("cb").Descendants()
                      select new Hb(f.Attribute("host").Value);






public class Hb
{
       public string a{ get; set; }
        public string  aKey { get; set; }
        public string  bKey { get; set; } 
}





<cb rootElement><Hb xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">


      <host>host01</host>

      <keyF>
        <a>1</a>
        <aKey>job_id</aKey>
        <bKey>883b196a-6e2d-462a-9e3a-8a2021585629</bKey>
      </keyF>

      <field>
        <a>1</a>
        <aKey>asset_state</aKey>
        <bKey>STOPPED</bKey>
      </field>

      <field>
        <a>1</a>
        <aKey>channel</aKey>
        <bKey>1</bKey>
      </field>

      <field>
        <a>1</a>
        <aKey>timecode_out</aKey>
        <bKey>00:00:00.00</bKey>
      </field>

      <field>
        <a>1</a>
        <aKey>% Processor Time 0</aKey>
        <bKey>0.33</bKey>
      </field> 


    </Hb>
</cb>

1 个答案:

答案 0 :(得分:2)

hostElement(),而不是Attribute()

此外,写doc.Descendants("cb").Descendants()将返回<cb>内任何位置的每个元素 您可能想要更具体的电话

相关问题