How can I serialize a list of objects to XML

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Heres models:
<span style="font-size:small <span style="font-size:small
<pre class="prettyprint [XmlRoot("BaseClass")]
public abstract class BaseClass
{
}

[XmlRoot("DerivedClassA")]
public class DerivedClassA : BaseClass
{
[XmlElement("A_Property")]
public int A_Property { set; get; }
}

[XmlRoot("DerivedClassB")]
public class DerivedClassB: BaseClass
{
[XmlElement("B_Property")]
public int B_Property { set; get; }
}

[XmlRoot("UnitedBaseClass")]
public class UnitedBaseClass : BaseClass, ICollection<BaseClass>
{
//ICollection Implementations...
}[/code]


XmlSerializationModule:
<pre class="prettyprint public static class XMLSerializationModule
{
static Dictionary<Type, XmlSerializer> serializers = new Dictionary<Type, XmlSerializer>();

public static void Save<T>(T obj, string filePath)
{
using (var stream = File.Create(filePath))
{
var type = obj.GetType();
var serializer = GetSerializer(type);
serializer.Serialize(stream, obj);
}
}

public static T Load<T>(string filePath)
{

using (var stream = File.Open(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
{
try
{
var serializer = GetSerializer<T>();
return (T)serializer.Deserialize(stream);
}
catch
{
return default(T);
}
}
}

public static XmlSerializer GetSerializer<T>()
{
return GetSerializer(typeof(T));
}

public static XmlSerializer GetSerializer(Type type)
{
if (serializers.ContainsKey(type))
{
return serializers[type];
}
else
{
var serializer = new XmlSerializer(type);
serializers.Add(type, serializer);
return serializer;
}
}
}[/code]
<span style="font-size:small Now what I wanna do is like this:
<pre class="prettyprint var inst = new UnitedBaseClass();
inst.Add(new DerivedClassA());
inst.Add(new DerivedClassB());
XMLSerializationModule.Save(inst, "Test.xml");[/code]
But it will fail due to exception:
Unhandled Exception: System.InvalidOperationException: There was an error generating the XML document. ---> System.Inval<br/>
idOperationException: The type XMLSerializationTest.DerivedClassA was not expected. Use the XmlInclude<br/>
or SoapInclude attribute to specify types that are not known statically.
<span style="font-size:small So I added attribute "[XmlInclude(typeof(DerivedClassA))]" and "[XmlInclude(typeof(DerivedClassB))]" to class "UnitedBaseClass", and it works,
but the XML generated is not expected.
<pre class="prettyprint <UnitedBaseClass>
<BaseClass xsi:type="DerivedClassA" A_Property="0" />
<BaseClass xsi:type="DerivedClassB" B_Property="0" />
</UnitedBaseClass>[/code]
And the expected result:
<pre class="prettyprint <UnitedBaseClass>
<DerivedClassA A_Property="0" />
<DerivedClassB B_Property="0" />
</UnitedBaseClass>[/code]
Any idea? I dont want to use XElement or XmlElement to write them manually... XD
<span style="font-size:small <br/>


<span style="font-size:small

<span style="font-size:small





View the full article
 
Back
Top