Sayo Oladeji
Sayo Oladeji

Reputation: 741

Problems Serializing Object

I have this class in C#

public class ExerciseDTO {
    Dictionary<String, String> qa;
    private String materialId, content, topic;

    //constructors

    //properties

    public void makePersistent(String path) {
        Stream outputStream = File.OpenWrite(path + @"\" + this.topic + ".xml");
        XmlSerializer serializer = new XmlSerializer(typeof(ExerciseDTO));
        serializer.Serialize(outputStream, this);
        outputStream.Flush();
        outputStream.Close();
    }
}

Calling the makePersistent method makes the app throw an exeption with message An error occured while reflecting object of type ExerciseDTO. Why can't I serialize instances of this class?

Upvotes: 0

Views: 476

Answers (3)

David
David

Reputation: 16287

[Serializable]
public class ExerciseDTO {
...
}
  • You should also have a default (argument free) constructor
  • You should also make your property public, if you want to automatically serialize it
  • serialization of a Dictionary<> is not supported by .net, there are however workrounds, for instance this post.

Edit: as commented below: it should be "serialization of a Dictionary<> is not supported by the XmlSerializer class".

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1039398

Why can't I serialize instances of this class?

Because the XmlSerializer class doesn't support serializing Dictionary<TKey, TValue> properties.

As an alternative you could use the DataContractSerializer which supports dictionaries.

Upvotes: 4

bas
bas

Reputation: 14982

What Darin said, the Dictionary is not XML serializable. You can create your own XmlSerializableDictionary. There are plenty good examples on the web. Here is one:

http://weblogs.asp.net/pwelter34/archive/2006/05/03/444961.aspx

In case the link dies in the future, a copy/paste:

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;

[XmlRoot("dictionary")]
public class SerializableDictionary<TKey, TValue>
    : Dictionary<TKey, TValue>, IXmlSerializable
{
    #region IXmlSerializable Members

    public System.Xml.Schema.XmlSchema GetSchema()
    {
        return null;
    }

    public void ReadXml(System.Xml.XmlReader reader)
    {
        XmlSerializer keySerializer = new XmlSerializer(typeof(TKey));
        XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue));
        bool wasEmpty = reader.IsEmptyElement;

        reader.Read();

        if (wasEmpty)
            return;

        while (reader.NodeType != System.Xml.XmlNodeType.EndElement)
        {
            reader.ReadStartElement("item");
            reader.ReadStartElement("key");

            TKey key = (TKey)keySerializer.Deserialize(reader);

            reader.ReadEndElement();
            reader.ReadStartElement("value");

            TValue value = (TValue)valueSerializer.Deserialize(reader);
            reader.ReadEndElement();
            this.Add(key, value);
            reader.ReadEndElement();
            reader.MoveToContent();
        }

        reader.ReadEndElement();
    } 

    public void WriteXml(System.Xml.XmlWriter writer)
    {
        XmlSerializer keySerializer = new XmlSerializer(typeof(TKey));

        XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue));

        foreach (TKey key in this.Keys)
        {
            writer.WriteStartElement("item");

            writer.WriteStartElement("key");
            keySerializer.Serialize(writer, key);
            writer.WriteEndElement();
            writer.WriteStartElement("value");
            TValue value = this[key];
            valueSerializer.Serialize(writer, value);
            writer.WriteEndElement();
            writer.WriteEndElement();
        }
    }
    #endregion
}

Upvotes: 0

Related Questions