/*
* SerializableGenerics
* Copyright (c) 2009, Eduardo Sanchez-Ros
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.Xml.Serialization;
using System.Xml.Schema;
using System.Xml;
namespace SerializableGenerics
{
/// <summary>
/// Represents a serializable collection of key/value pairs that are sorted on the key.
/// </summary>
/// <typeparam name="TKey">The type of the keys on the dictionary.</typeparam>
/// <typeparam name="TValue">The type of the values on the dictionary.</typeparam>
public class SerializableSortedDictionary<TKey, TValue> : SortedDictionary<TKey, TValue>, IXmlSerializable
{
// store key and value types
private readonly Type m_tKey = typeof(TKey);
private readonly Type m_tValue = typeof(TValue);
/// <summary>
/// Returns a string that represents the current SerializableDictionary.
/// </summary>
/// <returns>
/// A string that represents the current SerializableDictionary.
/// </returns>
public override string ToString()
{
return SerializableGenerics.GetTypeName(GetType());
}
#region IXmlSerializable Members
/// <summary>
/// This property is reserved, apply the System.Xml.Serialization.XmlSchemaProviderAttribute
/// to the class instead.
/// </summary>
/// <returns>
/// An System.Xml.Schema.XmlSchema that describes the XML representation of the
/// object that is produced by the System.Xml.Serialization.IXmlSerializable.WriteXml(System.Xml.XmlWriter)
/// method and consumed by the System.Xml.Serialization.IXmlSerializable.ReadXml(System.Xml.XmlReader)
/// method.
/// </returns>
XmlSchema IXmlSerializable.GetSchema()
{
return null;
}
/// <summary>
/// Generates an object from its XML representation.
/// </summary>
/// <param name="reader">The System.Xml.XmlReader stream from which the object is deserialized.</param>
void IXmlSerializable.ReadXml(XmlReader reader)
{
// Create xml serializers for key and value
XmlSerializer keySerializer = new XmlSerializer(m_tKey);
XmlSerializer valueSerializer = new XmlSerializer(m_tValue);
// Get key-value pair name
string keyValuePairName = SerializableGenerics.GetKeyValuePairName(m_tKey, m_tValue);
// Read start element and move to content
reader.ReadStartElement();
reader.MoveToContent();
// Is keyValuePairName the start element
if (!reader.IsStartElement(keyValuePairName))
{
// Throw an exception
throw new XmlException("Starting element " + keyValuePairName + " not found.");
}
// Loop through key-value pairs
while (reader.IsStartElement(keyValuePairName))
{
// Read key-value pair and move to content
reader.ReadStartElement(keyValuePairName);
reader.MoveToContent();
// Deserialize key and value
TKey key = (TKey)keySerializer.Deserialize(reader);
TValue value = (TValue)valueSerializer.Deserialize(reader);
// Read end element and add key-value pair to dictionary
reader.ReadEndElement();
Add(key, value);
}
// Read end element and move to content
reader.ReadEndElement();
reader.MoveToContent();
}
/// <summary>
/// Converts an object into its XML representation.
/// </summary>
/// <param name="writer">The System.Xml.XmlWriter stream to which the object is serialized.</param>
void IXmlSerializable.WriteXml(XmlWriter writer)
{
// Create xml serializers for key and value
XmlSerializer keySerializer = new XmlSerializer(m_tKey);
XmlSerializer valueSerializer = new XmlSerializer(m_tValue);
// Get key-value pair name
string keyValuePairName = SerializableGenerics.GetKeyValuePairName(m_tKey, m_tValue);
Enumerator enumerator = GetEnumerator();
while (enumerator.MoveNext())
{
// Get current key value pair
KeyValuePair<TKey, TValue> keyValuePair = enumerator.Current;
// Write start element with key-value pair name
writer.WriteStartElement(keyValuePairName);
// Serialize key and value
keySerializer.Serialize(writer, keyValuePair.Key);
valueSerializer.Serialize(writer, keyValuePair.Value);
// Write end element with key-value pair name
writer.WriteEndElement();
}
}
#endregion
}
}
Sunday, 27 March 2011
Serializing Generics - SerializableSortedDictionary<T>
The SerializableSortedDictionary<T> class extends SortedDictionary<T> and implements IXmlSerializable, allowing you to serialize a generic stack into XML:
Serializing Generics - SerializableSortedList<T>
The SerializableSortedList<T> class extends SortedList<T> and implements IXmlSerializable, allowing you to serialize a generic sorted list into XML:
/*
* SerializableGenerics
* Copyright (c) 2009, Eduardo Sanchez-Ros
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.Xml.Serialization;
using System.Xml.Schema;
using System.Xml;
namespace SerializableGenerics
{
/// <summary>
/// Represents a serializable collection of key/value pairs that are sorted by key based on
/// the associated System.Collections.Generic.IComparer<T> implementation.
/// </summary>
/// <typeparam name="TKey">The type of the keys on the dictionary.</typeparam>
/// <typeparam name="TValue">The type of the values on the dictionary.</typeparam>
public class SerializableSortedList<TKey, TValue> : SortedList<TKey, TValue>, IXmlSerializable
{
// store key and value types
private readonly Type m_tKey = typeof(TKey);
private readonly Type m_tValue = typeof(TValue);
/// <summary>
/// Returns a string that represents the current SerializableDictionary.
/// </summary>
/// <returns>
/// A string that represents the current SerializableDictionary.
/// </returns>
public override string ToString()
{
return SerializableGenerics.GetTypeName(GetType());
}
#region IXmlSerializable Members
/// <summary>
/// This property is reserved, apply the System.Xml.Serialization.XmlSchemaProviderAttribute
/// to the class instead.
/// </summary>
/// <returns>
/// An System.Xml.Schema.XmlSchema that describes the XML representation of the
/// object that is produced by the System.Xml.Serialization.IXmlSerializable.WriteXml(System.Xml.XmlWriter)
/// method and consumed by the System.Xml.Serialization.IXmlSerializable.ReadXml(System.Xml.XmlReader)
/// method.
/// </returns>
XmlSchema IXmlSerializable.GetSchema()
{
return null;
}
/// <summary>
/// Generates an object from its XML representation.
/// </summary>
/// <param name="reader">The System.Xml.XmlReader stream from which the object is deserialized.</param>
void IXmlSerializable.ReadXml(XmlReader reader)
{
// Create xml serializers for key and value
XmlSerializer keySerializer = new XmlSerializer(m_tKey);
XmlSerializer valueSerializer = new XmlSerializer(m_tValue);
// Get key-value pair name
string keyValuePairName = SerializableGenerics.GetKeyValuePairName(m_tKey, m_tValue);
// Read start element and move to content
reader.ReadStartElement();
reader.MoveToContent();
// Is keyValuePairName the start element
if (!reader.IsStartElement(keyValuePairName))
{
// Throw an exception
throw new XmlException("Starting element " + keyValuePairName + " not found.");
}
// Loop through key-value pairs
while (reader.IsStartElement(keyValuePairName))
{
// Read key-value pair and move to content
reader.ReadStartElement(keyValuePairName);
reader.MoveToContent();
// Deserialize key and value
TKey key = (TKey)keySerializer.Deserialize(reader);
TValue value = (TValue)valueSerializer.Deserialize(reader);
// Read end element and add key-value pair to dictionary
reader.ReadEndElement();
Add(key, value);
}
// Read end element and move to content
reader.ReadEndElement();
reader.MoveToContent();
}
/// <summary>
/// Converts an object into its XML representation.
/// </summary>
/// <param name="writer">The System.Xml.XmlWriter stream to which the object is serialized.</param>
void IXmlSerializable.WriteXml(XmlWriter writer)
{
// Create xml serializers for key and value
XmlSerializer keySerializer = new XmlSerializer(m_tKey);
XmlSerializer valueSerializer = new XmlSerializer(m_tValue);
// Get key-value pair name
string keyValuePairName = SerializableGenerics.GetKeyValuePairName(m_tKey, m_tValue);
IEnumerator<KeyValuePair<TKey, TValue>> enumerator = GetEnumerator();
while (enumerator.MoveNext())
{
// Get current key value pair
KeyValuePair<TKey, TValue> keyValuePair = enumerator.Current;
// Write start element with key-value pair name
writer.WriteStartElement(keyValuePairName);
// Serialize key and value
keySerializer.Serialize(writer, keyValuePair.Key);
valueSerializer.Serialize(writer, keyValuePair.Value);
// Write end element with key-value pair name
writer.WriteEndElement();
}
}
#endregion
}
}
Serializing Generics - SerializableStack<T>
The SerializableStack<T> class extends Stack<T> and implements IXmlSerializable, allowing you to serialize a generic stack into XML:
The previous class uses the SerializableGenerics helper class which constructs the element name based on the key-value pair node.
/*
* SerializableGenerics
* Copyright (c) 2009, Eduardo Sanchez-Ros
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.Xml.Serialization;
using System.Xml.Schema;
using System.Xml;
namespace SerializableGenerics
{
/// <summary>
/// Represents a variable size last-in-first-out (LIFO) serializable collection of instances
/// of the same arbitrary type.
/// </summary>
/// <typeparam name="T">The type of the items on the linked list.</typeparam>
public class SerializableStack<T> : Stack<T>, IXmlSerializable
{
// store list type
private readonly Type m_type = typeof(T);
/// <summary>
/// Returns a string that represents the current SerializableDictionary.
/// </summary>
/// <returns>
/// A string that represents the current SerializableDictionary.
/// </returns>
public override string ToString()
{
return SerializableGenerics.GetTypeName(GetType());
}
#region IXmlSerializable Members
/// <summary>
/// This property is reserved, apply the System.Xml.Serialization.XmlSchemaProviderAttribute
/// to the class instead.
/// </summary>
/// <returns>
/// An System.Xml.Schema.XmlSchema that describes the XML representation of the
/// object that is produced by the System.Xml.Serialization.IXmlSerializable.WriteXml(System.Xml.XmlWriter)
/// method and consumed by the System.Xml.Serialization.IXmlSerializable.ReadXml(System.Xml.XmlReader)
/// method.
/// </returns>
XmlSchema IXmlSerializable.GetSchema()
{
return null;
}
/// <summary>
/// Generates an object from its XML representation.
/// </summary>
/// <param name="reader">The System.Xml.XmlReader stream from which the object is deserialized.</param>
void IXmlSerializable.ReadXml(XmlReader reader)
{
// Create xml serializer for type
XmlSerializer typeSerializer = new XmlSerializer(m_type);
// Read start element and move to content
reader.ReadStartElement();
reader.MoveToContent();
// Loop through elements
while (reader.NodeType != XmlNodeType.EndElement)
{
// Deserialize type
T item = (T)typeSerializer.Deserialize(reader);
// Add node to stack
Push(item);
}
// Read end element and move to content
reader.ReadEndElement();
reader.MoveToContent();
}
/// <summary>
/// Converts an object into its XML representation.
/// </summary>
/// <param name="writer">The System.Xml.XmlWriter stream to which the object is serialized.</param>
void IXmlSerializable.WriteXml(XmlWriter writer)
{
// Create xml serializer for type
XmlSerializer typeSerializer = new XmlSerializer(m_type);
IEnumerator<T> enumerator = GetEnumerator();
while (enumerator.MoveNext())
{
// Serialize type
typeSerializer.Serialize(writer, enumerator.Current);
}
}
#endregion
}
}
The previous class uses the SerializableGenerics helper class which constructs the element name based on the key-value pair node.
Serializing Generics
The following exceptions are thrown when you try to serialize a any of the types (except for List) in the System.Collections.Generic namespace:
A first chance exception of type 'System.NotSupportedException' occurred in System.Xml.dll
System.NotSupportedException: The type System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] is not supported because it implements IDictionary.
A first chance exception of type 'System.InvalidOperationException' occurred in System.Xml.dll
System.InvalidOperationException: You must implement a default accessor on System.Collections.Generic.Queue`1[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] because it inherits from ICollection.
at System.Xml.Serialization.TypeScope.GetDefaultIndexer(Type type, String memberInfo)
at System.Xml.Serialization.TypeScope.ImportTypeDesc(Type type, MemberInfo memberInfo, Boolean directReference)
at System.Xml.Serialization.TypeScope.GetTypeDesc(Type type, MemberInfo source, Boolean directReference, Boolean throwOnError)
at System.Xml.Serialization.ModelScope.GetTypeModel(Type type, Boolean directReference)
at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(Type type, XmlRootAttribute root, String defaultNamespace)
at System.Xml.Serialization.XmlSerializer..ctor(Type type, String defaultNamespace)
at System.Xml.Serialization.XmlSerializer..ctor(Type type)
A way around this problem is to implement the IXmlSerializable interface.
The following classes will allow you serialize generic types to XML in .NET 2.0:
You can download the library's source code here.
The previous classes use the SerializableGenerics helper class which constructs the element name based on the key-value pair node:
A first chance exception of type 'System.NotSupportedException' occurred in System.Xml.dll
System.NotSupportedException: The type System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] is not supported because it implements IDictionary.
A first chance exception of type 'System.InvalidOperationException' occurred in System.Xml.dll
System.InvalidOperationException: You must implement a default accessor on System.Collections.Generic.Queue`1[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] because it inherits from ICollection.
at System.Xml.Serialization.TypeScope.GetDefaultIndexer(Type type, String memberInfo)
at System.Xml.Serialization.TypeScope.ImportTypeDesc(Type type, MemberInfo memberInfo, Boolean directReference)
at System.Xml.Serialization.TypeScope.GetTypeDesc(Type type, MemberInfo source, Boolean directReference, Boolean throwOnError)
at System.Xml.Serialization.ModelScope.GetTypeModel(Type type, Boolean directReference)
at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(Type type, XmlRootAttribute root, String defaultNamespace)
at System.Xml.Serialization.XmlSerializer..ctor(Type type, String defaultNamespace)
at System.Xml.Serialization.XmlSerializer..ctor(Type type)
A way around this problem is to implement the IXmlSerializable interface.
The following classes will allow you serialize generic types to XML in .NET 2.0:
- SerializableDictionary<T>
- SerializableLinkedList<T>
- SerializableQueue<T>
- SerializableSortedDictionary<T>
- SerializableSortedList<T>
- SerializableStack<T>
You can download the library's source code here.
The previous classes use the SerializableGenerics helper class which constructs the element name based on the key-value pair node:
/*
* SerializableGenerics
* Copyright (c) 2009, Eduardo Sanchez-Ros
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
namespace SerializableGenerics
{
public static class SerializableGenerics
{
private const string OF = "Of";
/// <summary>
/// Gets the key-value pair name
/// </summary>
/// <param name="tKey"></param>
/// <param name="tValue"></param>
/// <returns></returns>
public static string GetKeyValuePairName(Type tKey, Type tValue)
{
// return key-value pair name
return GetTypeName(tKey) + GetTypeName(tValue);
}
/// <summary>
/// Returns the name of the type
/// </summary>
/// <param name="type">Type to generate the name from</param>
/// <returns>The name of the type</returns>
public static String GetTypeName(Type type)
{
String typeName;
// Is type generic
if (type.IsGenericType)
{
// Get type name - Generics
typeName = type.Name.Substring(0, type.Name.Length - 2);
typeName += OF;
// Get type's arguments
Type[] types = type.GetGenericArguments();
foreach (Type t in types)
{
// Append type's name
typeName += GetTypeName(t);
}
}
else if (type.IsArray)
{
// Compose array name as "ArrayOf" and get array element's type name
typeName = type.BaseType.Name;
typeName += OF;
typeName += GetTypeName(type.GetElementType());
}
else
{
// Append type's name
typeName = type.Name;
}
// return type's name
return typeName;
}
}
}
Serializing Generics - SerializableQueue<T>
The SerializableQueue class extends Queue<T> and implements IXmlSerializable, allowing you to serialize a generic queue into XML:
The previous class uses the SerializableGenerics helper class which constructs the element name based on the key-value pair node.
/*
* SerializableGenerics
* Copyright (c) 2009, Eduardo Sanchez-Ros
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.Xml.Serialization;
using System.Xml.Schema;
using System.Xml;
namespace SerializableGenerics
{
/// <summary>
/// Represents a first-in, first-out serializable collection of objects.
/// </summary>
/// <typeparam name="T">The type of the items on the queue.</typeparam>
public class SerializableQueue<T> : Queue<T>, IXmlSerializable
{
// store list type
private readonly Type m_type = typeof(T);
/// <summary>
/// Returns a string that represents the current SerializableDictionary.
/// </summary>
/// <returns>
/// A string that represents the current SerializableDictionary.
/// </returns>
public override string ToString()
{
return SerializableGenerics.GetTypeName(GetType());
}
#region IXmlSerializable Members
/// <summary>
/// This property is reserved, apply the System.Xml.Serialization.XmlSchemaProviderAttribute
/// to the class instead.
/// </summary>
/// <returns>
/// An System.Xml.Schema.XmlSchema that describes the XML representation of the
/// object that is produced by the System.Xml.Serialization.IXmlSerializable.WriteXml(System.Xml.XmlWriter)
/// method and consumed by the System.Xml.Serialization.IXmlSerializable.ReadXml(System.Xml.XmlReader)
/// method.
/// </returns>
XmlSchema IXmlSerializable.GetSchema()
{
return null;
}
/// <summary>
/// Generates an object from its XML representation.
/// </summary>
/// <param name="reader">The System.Xml.XmlReader stream from which the object is deserialized.</param>
void IXmlSerializable.ReadXml(XmlReader reader)
{
// Create xml serializer for type
XmlSerializer typeSerializer = new XmlSerializer(m_type);
// Read start element and move to content
reader.ReadStartElement();
reader.MoveToContent();
// Loop through elements
while (reader.NodeType != XmlNodeType.EndElement)
{
// Deserialize type
T item = (T)typeSerializer.Deserialize(reader);
// Enqueue item on queue
Enqueue(item);
}
// Read end element and move to content
reader.ReadEndElement();
reader.MoveToContent();
}
/// <summary>
/// Converts an object into its XML representation.
/// </summary>
/// <param name="writer">The System.Xml.XmlWriter stream to which the object is serialized.</param>
void IXmlSerializable.WriteXml(XmlWriter writer)
{
// Create xml serializer for type
XmlSerializer typeSerializer = new XmlSerializer(m_type);
IEnumerator<T> enumerator = GetEnumerator();
while (enumerator.MoveNext())
{
// Serialize type
typeSerializer.Serialize(writer, enumerator.Current);
}
}
#endregion
}
}
The previous class uses the SerializableGenerics helper class which constructs the element name based on the key-value pair node.
Serializing Generics - SerializableLinkedList<T>
The SerializableLinkedList<T> class extends LinkedList<T> and implements IXmlSerializable, allowing you to serialize a generic linked list into XML:
The previous class uses the SerializableGenerics helper class which constructs the element name based on the key-value pair node.
/*
* SerializableGenerics
* Copyright (c) 2009, Eduardo Sanchez-Ros
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.Xml.Serialization;
using System.Xml.Schema;
using System.Xml;
namespace SerializableGenerics
{
/// <summary>
/// Represents a strongly typed serializable list of objects that can be accessed by index.
/// </summary>
/// <typeparam name="T">The type of the items on the linked list.</typeparam>
public class SerializableLinkedList<T> : LinkedList<T>, IXmlSerializable
{
// store list type
private readonly Type m_type = typeof(T);
/// <summary>
/// Returns a string that represents the current SerializableDictionary.
/// </summary>
/// <returns>
/// A string that represents the current SerializableDictionary.
/// </returns>
public override string ToString()
{
return SerializableGenerics.GetTypeName(GetType());
}
#region IXmlSerializable Members
/// <summary>
/// This property is reserved, apply the System.Xml.Serialization.XmlSchemaProviderAttribute
/// to the class instead.
/// </summary>
/// <returns>
/// An System.Xml.Schema.XmlSchema that describes the XML representation of the
/// object that is produced by the System.Xml.Serialization.IXmlSerializable.WriteXml(System.Xml.XmlWriter)
/// method and consumed by the System.Xml.Serialization.IXmlSerializable.ReadXml(System.Xml.XmlReader)
/// method.
/// </returns>
XmlSchema IXmlSerializable.GetSchema()
{
return null;
}
/// <summary>
/// Generates an object from its XML representation.
/// </summary>
/// <param name="reader">The System.Xml.XmlReader stream from which the object is deserialized.</param>
void IXmlSerializable.ReadXml(XmlReader reader)
{
// Create xml serializer for type
XmlSerializer typeSerializer = new XmlSerializer(m_type);
// Read start element and move to content
reader.ReadStartElement();
reader.MoveToContent();
// Loop through elements
while (reader.NodeType != XmlNodeType.EndElement)
{
// Deserialize type
T value = (T)typeSerializer.Deserialize(reader);
// Create a liked list node of T
LinkedListNode<T> node = new LinkedListNode<T>(value);
// Add node to linked list
AddLast(node);
}
// Read end element and move to content
reader.ReadEndElement();
reader.MoveToContent();
}
/// <summary>
/// Converts an object into its XML representation.
/// </summary>
/// <param name="writer">The System.Xml.XmlWriter stream to which the object is serialized.</param>
void IXmlSerializable.WriteXml(XmlWriter writer)
{
// Create xml serializer for type
XmlSerializer typeSerializer = new XmlSerializer(m_type);
IEnumerator<T> enumerator = GetEnumerator();
while (enumerator.MoveNext())
{
// Serialize type
typeSerializer.Serialize(writer, enumerator.Current);
}
}
#endregion
}
}
The previous class uses the SerializableGenerics helper class which constructs the element name based on the key-value pair node.
Thursday, 24 March 2011
Serializing Generics - SerializableDictionary<T>
The SerializableDictionary<T> class extends Dictionary<T> and implements IXmlSerializable, allowing you to serialize a generic dictionary into XML. The code was first inspired on Paul Welter's article XML Serializable Generic Dictionary:
As an example, let's say we have a Serializable Dictionary of Strings and String Arrays. The type will be declared as follows:
And the resulting serialized XML:
/*
* SerializableGenerics
* Copyright (c) 2009, Eduardo Sanchez-Ros
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.Xml.Serialization;
using System.Xml.Schema;
using System.Xml;
namespace SerializableGenerics
{
/// <summary>
/// Represents a serializable collection of keys and values.
/// </summary>
/// <typeparam name="TKey">The type of the keys on the dictionary.</typeparam>
/// <typeparam name="TValue">The type of the values on the dictionary.</typeparam>
public class SerializableDictionary<TKey, TValue> : Dictionary<TKey, TValue>, IXmlSerializable
{
// store key and value types
private readonly Type m_tKey = typeof(TKey);
private readonly Type m_tValue = typeof(TValue);
/// <summary>
/// Returns a string that represents the current SerializableDictionary.
/// </summary>
/// <returns>
/// A string that represents the current SerializableDictionary.
/// </returns>
public override string ToString()
{
return SerializableGeneric.GetTypeName(GetType());
}
#region IXmlSerializable Members
/// <summary>
/// This property is reserved, apply the System.Xml.Serialization.XmlSchemaProviderAttribute
/// to the class instead.
/// </summary>
/// <returns>
/// An System.Xml.Schema.XmlSchema that describes the XML representation of the
/// object that is produced by the System.Xml.Serialization.IXmlSerializable.WriteXml(System.Xml.XmlWriter)
/// method and consumed by the System.Xml.Serialization.IXmlSerializable.ReadXml(System.Xml.XmlReader)
/// method.
/// </returns>
XmlSchema IXmlSerializable.GetSchema()
{
return null;
}
/// <summary>
/// Generates an object from its XML representation.
/// </summary>
/// <param name="reader">The System.Xml.XmlReader stream from which the object is deserialized.</param>
void IXmlSerializable.ReadXml(XmlReader reader)
{
// Create xml serializers for key and value
XmlSerializer keySerializer = new XmlSerializer(m_tKey);
XmlSerializer valueSerializer = new XmlSerializer(m_tValue);
// Get key-value pair name
string keyValuePairName = SerializableGeneric.GetKeyValuePairName(m_tKey, m_tValue);
// Read start element and move to content
reader.ReadStartElement();
reader.MoveToContent();
// Is keyValuePairName the start element
if (!reader.IsStartElement(keyValuePairName))
{
// Throw an exception
throw new XmlException("Starting element " + keyValuePairName + " not found.");
}
// Loop through key-value pairs
while (reader.IsStartElement(keyValuePairName))
{
// Read key-value pair and move to content
reader.ReadStartElement(keyValuePairName);
reader.MoveToContent();
// Deserialize key and value
TKey key = (TKey)keySerializer.Deserialize(reader);
TValue value = (TValue)valueSerializer.Deserialize(reader);
// Read end element and add key-value pair to dictionary
reader.ReadEndElement();
Add(key, value);
}
// Read end element and move to content
reader.ReadEndElement();
reader.MoveToContent();
}
/// <summary>
/// Converts an object into its XML representation.
/// </summary>
/// <param name="writer">The System.Xml.XmlWriter stream to which the object is serialized.</param>
void IXmlSerializable.WriteXml(XmlWriter writer)
{
// Create xml serializers for key and value
XmlSerializer keySerializer = new XmlSerializer(m_tKey);
XmlSerializer valueSerializer = new XmlSerializer(m_tValue);
// Get key-value pair name
string keyValuePairName = SerializableGeneric.GetKeyValuePairName(m_tKey, m_tValue);
Enumerator enumerator = GetEnumerator();
while (enumerator.MoveNext())
{
// Get current key value pair
KeyValuePair<TKey, TValue> keyValuePair = enumerator.Current;
// Write start element with key-value pair name
writer.WriteStartElement(keyValuePairName);
// Serialize key and value
keySerializer.Serialize(writer, keyValuePair.Key);
valueSerializer.Serialize(writer, keyValuePair.Value);
// Write end element with key-value pair name
writer.WriteEndElement();
}
}
#endregion
}
}
As an example, let's say we have a Serializable Dictionary of Strings and String Arrays. The type will be declared as follows:
SerializableDictionary<String, String[]>
And the resulting serialized XML:
Key #0 Value 1 of 0 Value 2 of 0 Value 3 of 0 Key #1 Value 1 of 1 Value 2 of 1 Value 3 of 1
Subscribe to:
Comments (Atom)