2011-02-03 70 views
0

我從XmlSerialization升級到Linq-to-xml,發現很難從此結構中獲取對象。XDocument LINQ複雜語法

我從這篇文章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> 

回答

2

host是一個Element(),不是Attribute()

此外,編寫doc.Descendants("cb").Descendants()將返回<cb>內的任何位置的每個元素。
您可能想要更具體的電話

+0

排序。乾杯! – bizl 2011-02-03 17:02:42