XmlSerializer doesn't serialize properties when inheriting List<T>? [modified]
-
Hi folks! The XmlSerializer is giving me a headache. I've got this class (.NET 2.0):
[Serializable]
public class ParticipantList : List<Participant>
{
private Guid _ID = Guid.NewGuid();
public Guid ID
{
get { return _ID; }
set { _ID = value; }
}public static void ToFile(ParticipantList list, string filename)
{
using (FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write))
{
XmlSerializer xs = new XmlSerializer(typeof(ParticipantList));
xs.Serialize(fs, list);
}
}public static ParticipantList FromFile(string filename)
{
using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
XmlSerializer xs = new XmlSerializer(typeof(ParticipantList));
return (ParticipantList)xs.Deserialize(fs);
}
}
}Pretty basic stuff it would seem. The participant list gets serialized OK, but the ID isn't included in the XML file! :( Did I miss something? Has the XmlSerializer gone nuts? Thanks in advance for tips on how to get the ID serialized as well! [Update] No additional property or member gets serialized, the Guid isn't the reason.
Regards, mav -- Black holes are the places where God divided by 0...
modified on Wednesday, February 17, 2010 6:16 AM
-
Hi folks! The XmlSerializer is giving me a headache. I've got this class (.NET 2.0):
[Serializable]
public class ParticipantList : List<Participant>
{
private Guid _ID = Guid.NewGuid();
public Guid ID
{
get { return _ID; }
set { _ID = value; }
}public static void ToFile(ParticipantList list, string filename)
{
using (FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write))
{
XmlSerializer xs = new XmlSerializer(typeof(ParticipantList));
xs.Serialize(fs, list);
}
}public static ParticipantList FromFile(string filename)
{
using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
XmlSerializer xs = new XmlSerializer(typeof(ParticipantList));
return (ParticipantList)xs.Deserialize(fs);
}
}
}Pretty basic stuff it would seem. The participant list gets serialized OK, but the ID isn't included in the XML file! :( Did I miss something? Has the XmlSerializer gone nuts? Thanks in advance for tips on how to get the ID serialized as well! [Update] No additional property or member gets serialized, the Guid isn't the reason.
Regards, mav -- Black holes are the places where God divided by 0...
modified on Wednesday, February 17, 2010 6:16 AM
It works for me, here is my test code :
using System;
using System.IO;
using System.Xml.Serialization;internal class Program
{
private static void Main()
{
const string FILENAME = @"d:\test123.xml";MyClass myClassBeforeSerializing = new MyClass(); XmlSerializer xmlSerializer = new XmlSerializer(typeof (MyClass)); using (FileStream fs = new FileStream(FILENAME, FileMode.Create)) { xmlSerializer.Serialize(fs, myClassBeforeSerializing); } MyClass myClassAfterSerializing = new MyClass(); using (FileStream fs = new FileStream(FILENAME, FileMode.Open)) { myClassAfterSerializing = (MyClass) xmlSerializer.Deserialize(fs); } Console.WriteLine(myClassBeforeSerializing.MyGuid.ToString()); Console.WriteLine(myClassAfterSerializing.MyGuid.ToString());
}
}[Serializable]
public class MyClass
{
private Guid _myGuid = Guid.NewGuid();public Guid MyGuid
{
get { return _myGuid; }
set { _myGuid = value; }
}
} -
It works for me, here is my test code :
using System;
using System.IO;
using System.Xml.Serialization;internal class Program
{
private static void Main()
{
const string FILENAME = @"d:\test123.xml";MyClass myClassBeforeSerializing = new MyClass(); XmlSerializer xmlSerializer = new XmlSerializer(typeof (MyClass)); using (FileStream fs = new FileStream(FILENAME, FileMode.Create)) { xmlSerializer.Serialize(fs, myClassBeforeSerializing); } MyClass myClassAfterSerializing = new MyClass(); using (FileStream fs = new FileStream(FILENAME, FileMode.Open)) { myClassAfterSerializing = (MyClass) xmlSerializer.Deserialize(fs); } Console.WriteLine(myClassBeforeSerializing.MyGuid.ToString()); Console.WriteLine(myClassAfterSerializing.MyGuid.ToString());
}
}[Serializable]
public class MyClass
{
private Guid _myGuid = Guid.NewGuid();public Guid MyGuid
{
get { return _myGuid; }
set { _myGuid = value; }
}
}Hi! Thanks for your reply. Meanwhile I've found that the problem doesn't stem from the property being a Guid but from deriving from List<T>. No matter which additional properties I define, none of them gets serialized!
Regards, mav -- Black holes are the places where God divided by 0...
-
Hi folks! The XmlSerializer is giving me a headache. I've got this class (.NET 2.0):
[Serializable]
public class ParticipantList : List<Participant>
{
private Guid _ID = Guid.NewGuid();
public Guid ID
{
get { return _ID; }
set { _ID = value; }
}public static void ToFile(ParticipantList list, string filename)
{
using (FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write))
{
XmlSerializer xs = new XmlSerializer(typeof(ParticipantList));
xs.Serialize(fs, list);
}
}public static ParticipantList FromFile(string filename)
{
using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
XmlSerializer xs = new XmlSerializer(typeof(ParticipantList));
return (ParticipantList)xs.Deserialize(fs);
}
}
}Pretty basic stuff it would seem. The participant list gets serialized OK, but the ID isn't included in the XML file! :( Did I miss something? Has the XmlSerializer gone nuts? Thanks in advance for tips on how to get the ID serialized as well! [Update] No additional property or member gets serialized, the Guid isn't the reason.
Regards, mav -- Black holes are the places where God divided by 0...
modified on Wednesday, February 17, 2010 6:16 AM
Ok, I've found a solution myself: According to this thread[^] the behaviour is "by design" and unsually no attributes of such a class are serialized. I've solved the problem by not deriving from List<T> but by including the list as a member. Thanks anyway,
Regards, mav -- Black holes are the places where God divided by 0...