0

I want to serialize multiple objects using XmlSerializer. Serializing operation runs okay but when I want to Deserialize objects, Deserialize only returns first object and if I call it again, it returns null!

I want my serialization to be custom so I implemented IXmlSerializabe :

public class Person : IXmlSerializable
{
    public int id;
    public string name;

    public virtual void WriteXml(XmlWriter writer)
    {
        Type type = this.GetType();
        FieldInfo[] pInfo = type.GetFields();

        foreach (FieldInfo property in pInfo)
        {
            object value = property.GetValue(this);
            writer.WriteElementString(property.Name, value.ToString());
        }
    }

    public virtual void ReadXml(XmlReader reader)
    {

        Type type = this.GetType();
        FieldInfo[] pInfo = type.GetFields();

        foreach (FieldInfo property in pInfo)
        {
            object value = null;
            reader.ReadToFollowing(property.Name);

            Type propertyType = property.FieldType;
            value = reader.ReadElementContentAs(property.FieldType, null);
            type.GetField(property.Name).SetValue(this, value);                
        }

        reader.Read();
    }

    public virtual XmlSchema GetSchema()
    {
        return (null);
    }  
}

And I call it this way:

        // Serialize
        Type type = typeof(Person);
        XmlSerializer serializer = new XmlSerializer(type);
        TextWriter writer = new StreamWriter(filename);

        foreach (Person c in list)
        {
            serializer.Serialize(writer, c);
        }
        writer.Close();

        // Deserialize
        XmlReader reader = XmlReader.Create(filename);
        var objectsList = serializer.Deserialize(reader);

Serialize creates following xml file :

    <?xml version="1.0" encoding="utf-8"?>
    <Person>
      <id>1</id>
      <name>name1</name>
    </Person><?xml version="1.0" encoding="utf-8"?>
    <Person>
      <id>2</id>
      <name>name2</name>
    </Person>

1 Answer 1

2

I found the answer. Corrected codes :

    public virtual void ReadXml(XmlReader reader)
    {

        Type type = this.GetType();
        FieldInfo[] pInfo = type.GetFields();

        // Added
        reader.ReadStartElement();

        foreach (FieldInfo property in pInfo)
        {
            object value = null;
            reader.ReadToFollowing(property.Name);

            Type propertyType = property.FieldType;
            value = reader.ReadElementContentAs(property.FieldType, null);
            type.GetField(property.Name).SetValue(this, value);                
        }

        // Added
        reader.ReadEndElement();
    }

    // Serialize
    Type type = typeof(List<Person>); //Changed Type to List of Persons
    XmlSerializer serializer = new XmlSerializer(type);
    TextWriter writer = new StreamWriter(filename);

    serializer.Serialize(writer, list);

    writer.Close();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.