Skip to content

A short C# code snippet that does XML serialization on a subclass which inherits SortedSet (IEnumerable)

License

Notifications You must be signed in to change notification settings

xwang233/serialization-code-snippet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Serilization in C#

Motivation

I have encounter some code that inherits SortedSet<T>, for example

public class Dius : SortedSet<int> 
{
	int a; 
	string b; 
}

I need to do serializations on this object. If you use C# DataContract Serializer or XML Serializer, you will find that only contents in SortedSet<int> will be serialized. Neither of the extra properties a or b will be serialized. This is because the class inherits a class from either IEnumerable or ICollection.

See

For JSON .NET

Basically, in most of these answers, the alternative is to redefine the class like this

public class Dius 
{
	int a; 
	string b; 
	SortedSet<int> collection; 
}

However, I CANNOT redefine the class definition in this problem. So I found another workaround, see

The idea is that, for DataContract Serializer, it will use customized IXmlSerializable interface methods to serialize the object.

This time, we define a Dius_Wrapper class that looks like the second definition above, and let the original class Dius implements the interface IXmlSerializable. The interface methods and the wrapper class will look like this

// All the code below is within class Dius, which implements IXmlSerializable
[DataContract]
private class Dius_Wrapper
{
    [DataMember]
    public int a;
    [DataMember]
    public string b;
    [DataMember]
    public SortedSet<Yem> collection;
}

public XmlSchema GetSchema()
{
    return null; 
}

public void ReadXml(XmlReader reader)
{
    List<Type> knownTypes = new List<Type>() { typeof(Dius) };
    DataContractSerializer ser = 
		new DataContractSerializer(typeof(Dius_Wrapper), knownTypes);
    reader.ReadString();
    Dius_Wrapper dw = (Dius_Wrapper)ser.ReadObject(reader, true);

    this.Clear();
    this.UnionWith(dw.collection);
    this.a = dw.a;
    this.b = dw.b;
}

public void WriteXml(XmlWriter writer)
{
    Dius_Wrapper dw = new Dius_Wrapper() 
	{ 
		collection = new SortedSet<Yem>(this), 
		a = this.a, 
		b = this.b 
	};
    List<Type> knownTypes = new List<Type>() { typeof(Dius) };
    DataContractSerializer ser = 
		new DataContractSerializer(typeof(Dius_Wrapper), knownTypes);
    ser.WriteObject(writer, dw);
}

This may not be the best solution, but it solves the problem without significant changes to the original code. You may download the code and run it yourself. Although I wrote this code for SortedSet<>, it should also work with other IEnumerable, such as List<>, hopefully.

License

MIT. Use at your own risk.

About

A short C# code snippet that does XML serialization on a subclass which inherits SortedSet (IEnumerable)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages