Question

I have an auto generated xml file with the following format.

<?xml version="1.0"?>
<School>
  <Classes numberOfFields="5">
    <Class name="10" dataType="double">
        <Section value="A"/>
        <Section value="B"/>
        <Section value="C"/>
    </Class>
    <Class dataType="double"/>
    <Class dataType="double"/>
    <Class dataType="double"/>
    <Class dataType="double"/>
  </Classes>
</School>

I deserialize using "XmlDeserializer" as follows

School schoolResult = (School)xmlSerializer.Deserialize(stream);

XmlRootElement contains a collection of "Class" under "Classes" tag and further each "Class" would contain Collection of "Section".

And in C#, I have declared like this to deserialize "Classes" into a List of classes.

[XmlArray("Classes")]
[XmlArrayItem("Class", typeof(Class))]
public List<Class> Classes {};

Now to further deserialize Class into List of Sections, I have added the code as below.

[XmlArray("Class")]
[XmlArrayItem(ElementName="Section")]
public List<Section> ClassSections {};

My problem is I couldn't get the List of Sections values properly. Because I have "Class" as a class name in the first part and in second part I have mentioned same "Class" as Array element. So could any one tell how can I properly Deserialize my "School" object using "XmlSerializer" to get all the values properly.

Note: I cannot have a Array root tag like "Sections" under "Class". Because my xml document is auto generated. I cannot specify my own format.

Thanks...

Was it helpful?

Solution 2

    static void Main(string[] args)
    {
        var xml ="<?xml version=\"1.0\"?><School><Classes numberOfFields=\"5\"><Class name=\"10\" dataType=\"double\"><Section value=\"A\"/><Section value=\"B\"/><Section value=\"C\"/></Class><Class dataType=\"double\"/><Class dataType=\"double\"/><Class dataType=\"double\"/><Class dataType=\"double\"/></Classes></School>";
        School result;
        var serializer = new XmlSerializer(typeof(School));
        var xmlDoc = new XmlDocument();
        xmlDoc.LoadXml(xml);
        using (var reader = new XmlNodeReader(xmlDoc))
        {
            result = (School)serializer.Deserialize(reader);
        }
    }


public class School
{
    [XmlArray("Classes")]
    [XmlArrayItem("Class")]
    public List<Class> Classes { get; set; }
}

public class Class
{
    [XmlElement("Section")]
    public List<Section> ClassSections { get; set; }
}

public class Section
{
    [XmlAttribute("value")]
    public string Value { get; set; }
}

OTHER TIPS

try this :

    public class School
{
    [XmlAttribute]
    public int numberOfFields { get; set; }

    [XmlArray("Classes")]
    [XmlArrayItem("Class", typeof(Class))]
    public List<Class> Classes { get; set; }

}


public class Class
{
    [XmlAttribute]
    public string name { get; set; }
    [XmlAttribute]
    public string dataType { get; set; }


    [XmlElement("Section")]
    public List<Section> ClassSections { get; set; }

}

public class Section
{
    [XmlAttribute]
    public string value { get; set; }
}

* EDIT #1 **

--------------- Update Structure due to NumberOfFields is not readed ----------------

in my opinion it's not correct structure, but when u said it's an auto generated XML file, i think it simplest way to read it.

    public class School
{

    [XmlElement("Classes")]
    public List<ArrayClass> Classes { get; set; }

}

public class ArrayClass
{
    [XmlAttribute]
    public int numberOfFields { get; set; }

    [XmlElement("Class")]
    public List<Class> Class { get; set; }

} 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top