0

I am upgrading from XmlSerialization to Linq-to-xml and finding it hard to get objects from this structure.

I've tried suggestions from this post XmlSerializer - Deserialize different elements as collection of same element

but keep getting a null value trying to do this for the structures below. Thanks for your help.

  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>
Community
  • 1
  • 1
bizl
  • 1,535
  • 2
  • 12
  • 21

1 Answers1

2

host is an Element(), not an Attribute().

Also, writing doc.Descendants("cb").Descendants() will return every single element anywhere inside <cb>.
You probably want a more specific call

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964