【问题标题】:XDocument LINQ complex syntaxXDocument LINQ 复杂语法
【发布时间】:2011-02-03 16:20:12
【问题描述】:

我正在从 XmlSerialization 升级到 Linq-to-xml,发现很难从这个结构中获取对象。

我已经尝试过这篇帖子XmlSerializer - Deserialize different elements as collection of same element的建议

但尝试为下面的结构执行此操作时不断获得空值。感谢您的帮助。

  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>

【问题讨论】:

    标签: linq-to-xml


    【解决方案1】:

    hostElement(),而不是 Attribute()

    另外,写入doc.Descendants("cb").Descendants() 将返回&lt;cb&gt; 内任意位置的每一个元素。
    你可能想要一个更具体的电话

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-14
      • 1970-01-01
      • 2015-09-11
      相关资源
      最近更新 更多