JIng
JIng

Reputation: 11

Serializing IDictionary within Another Serializable Class

We have a class we are trying to serialize which contains a dictionary.

I have workable code implementing IXmlSerializable to serialize dictionaries but am a little lost as how to serialise the object using the default XMLSerializer and then when it reaches the dictionary element force it to use the custom built serializer.

I have, at present, discounted building a custom serializer for the entire object if I can help it, as the object may change over its lifetime and I was hoping to minimise the customisation which may cause future confusion.

Following is a cut down sample of the class I am trying to serialize, the actual object is much larger;

public class Report 
{
    public int ID { get; set; }
    public string Name { get; set; }

    //...

    private Dictionary<string, string> _parameters = new Dictionary<string, string>();

}

Any recommendation as to a simple approach to this would be aappreciated.

Upvotes: 1

Views: 147

Answers (2)

JIng
JIng

Reputation: 11

The original question arose as I was trying to find a workable solution for XML serialization of dictionaries (in particular thos resident in other objects).

In the meantime I have found an alternative option using the WCF DataContractSerializer which has the capability of serializing dictionaries. Simplest example is something like:

using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;

namespace CodeSample
{
class Report                        
{
   public int ID { get; set; }
   public string Name { get; set; }
   //...                                                  
   private Dictionary<string, string> _parameters = new Dictionary<string, string>();

   public Dictionary<string, string> Parameters {
       get { return _parameters; }
       set { _parameters = value; }       
   }
}


class StartUp
{
   static void Main()
   {
    System.IO.Stream fStream = new FileStream("C:\\Out.xml" , FileMode.Create);
    Report x = new Report();
    Report y;
    System.IO.Stream rStream;

    // Presuming that Parameters is an exposed reference to the dictionary
    x.Parameters.Add("Param1", "James2");
    x.Parameters.Add("Param2", System.DateTime.Now.ToString());
    x.Parameters.Add("Param3", 2.4.ToString());

    DataContractSerializer dcs = new DataContractSerializer(x.GetType());

    dcs.WriteObject(fStream, x);

    fStream.Flush();
    fStream.Close();

    rStream = new FileStream("C:\\Out.xml", FileMode.Open);

    y = (Report) dcs.ReadObject(rStream);
    //y is now a copy of x
   }
}

}

Not sure if there are any unforseen drawbacks.

Upvotes: 0

Marc Gravell
Marc Gravell

Reputation: 1062705

Unfortunately, IXmlSerializable is an all-or-nothing affair. In order to do anything yourself, you have to do everything for that type, which is not ideal.

To make it harder, the processor isn't able to handle much by way of generics, making it hard to use encapsulation of some type you control as a workaround.

Upvotes: 1

Related Questions