0

I got an XML file with this kind of structure:

<?xml version="1.0"?> 
<root>
    <first_element>
        <some_info></some_info>
    <other_info></other_info>
    </first_element>
    <second_element prop1="bla bla" prop2="bla bla" prop3="bla bla" ... prop50="bla bla">01</second_element>
    <second_element prop10="bla bla" prop11="bla bla" prop12="bla bla" ... prop50="bla bla">02</second_element>
    <second_element prop1="bla bla" prop2="bla bla" prop3="bla bla" ... prop60="bla bla">03</second_element>
</root>

I would like to deserialize the entire "second_element" content into a POCO class:

public class SecondElement
{
    [XmlText]
    public string ElementText { get; set; }

    //The Attribute list
    public List<AttributeObject> {get; set; }
}

public class AttributeObject
{
      public string Name {get ; set;} //Attribute Name
      public string Value {get ; set;} //Attribute Value 
}

I could have multiple attributes, until a maximum of 100, but the total items list is inconstant: sometimes attributes are missing, sometimes the list starts from the 10th element and so on.

Is there a way to achieve this whole content through deserialization? Thank you.

  • Look into Xml.Linq you can achieve this in a very handy way. https://stackoverflow.com/questions/5195728/best-way-to-read-through-xml – Spool Jan 19 '18 at 09:49
  • Thank you @Spool! Yes, querying with LINQ seems easy enough. What are the performance of LINQ against XMLSerialization? I'm curious. – ChrisLanzi Jan 19 '18 at 10:35
  • You can also use [`[XmlAnyAttribute]`](https://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlanyattributeattribute%28v=vs.110%29.aspx) to deserialize arbitrary attributes, for instance as shown in [*How to deserialize element with list of attributes in C#*](https://stackoverflow.com/q/32884364/3744182). – dbc Jan 21 '18 at 20:07

0 Answers0