Loading multiple XML elements into an object using serialization in C #

I am trying to load multiple elements with the same name from XML into a class using deserialization in C #. Everything in my example loads fine, but the array elements (Element3) are not being populated.

code:

class Program
{
    static void Main(string[] args)
    {
        FileStream file = new FileStream("service.xml", FileMode.Open);

        if (file != null)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(Service));
            Service service = (Service)serializer.Deserialize(file);
        }
    }
}

public class Service
{
    public bool Element1;
    public string Element2;
    public string[] Element3;
}

      

XML:

<Service>
    <Element1>true</Element1>
    <Element2>Text 1</Element2>
    <Element3>Text 2</Element3>
    <Element3>Text 3</Element3>
</Service>

      

+2


source to share


3 answers


Try to put [XmlElement]

on Element3

.



+4


source


The reason your array is not loading is because, as far as .NET XML Serialization is concerned, you are not trying to read the array. The array will be represented something like this:

<Element3Array>
    <ArrayElement>Text 2</ArrayElement>
    <ArrayElement>Text 3</ArrayElement>
</Element3Array>

      



You need to either change the format of the original XML or create a custom XML serializer for your class to handle your situation.

+2


source


I think your xml is wrong. Logically, the array is serialized like this:

<Element3>
    <string>Text 2</string>
    <string>Text 3</string>
</Element3>

      

So your xml should be in this format:

<Service>
    <Element1>true</Element1>
    <Element2>Text 1</Element2>
    <Element3>
        <string>Text 2</string>
        <string>Text 3</string>
    </Element3>
</Service>

      

Edit . Added an example to unserialize this xml if you cannot change the format. The code below is untested.

Your class service should be derived from IXmlSerializable :

public class Service : IXmlSerializable
{
    public System.Xml.Schema.XmlSchema  GetSchema()
    {
        return null;
    }

    public void  ReadXml(System.Xml.XmlReader reader)
    {
        List<string> element3 = new List<string>();

        while (reader.Read())
        {
        if (reader.Name == "Element1" && reader.NodeType == XmlNodeType.Element)
        {
            Element1 = Convert.ToBoolean(reader.ReadString());
        }
        else if (reader.Name == "Element2" && reader.NodeType == XmlNodeType.Element)
        {
            Element2 = reader.ReadString();
        }
        if (reader.Name == "Element1" && reader.NodeType == XmlNodeType.Element)
        {
            element3.Add(reader.ReadString());
        }
        }

        Element3 = element3.ToArray();
    }

    public void  WriteXml(System.Xml.XmlWriter writer)
    {
        writer.WriteStartElement ("Service"); 

        writer.WriteStartElement ("Element1"); 
        writer.WriteString(Element1.ToString());
        writer.WriteEndElement();

        writer.WriteStartElement ("Element2"); 
        writer.WriteString(Element2.ToString());
        writer.WriteEndElement();

        foreach (string ele in Element3)
        {
        writer.WriteStartElement ("Element3"); 
        writer.WriteString(ele);
        writer.WriteEndElement();
        }

        writer.WriteEndElement();
    }
}

      

+2


source







All Articles