Xml Serialization
-
Hi, What is the easiest way to serialize an array of objects like the one in the following code example?
public void Test()
{
List arObject = new List();
arObject.Add(Color.Red);
arObject.Add(Color.Green);
arObject.Add(new string("Hello World!"));
arObject.Add(new MyObject());// How to serialize to and from file here?
}In my program the object array represents an array of property values which could be of any time. Custom objects implement the
IXmlSerializable
interface, and simple types like strings appear to work fine. So far I have found problems with the low-level types likeSystem.Drawing.Color
and the like. Is there a generic way of serializing objects which do not implement theIXmlSerializable
interface? At the moment I am not using theXmlSerializer
class because I need extra control over how objects are serialized. So instead I am just using theXmlWriter
class. Any help would be greatly appreciated! Lea Hayes -
Hi, What is the easiest way to serialize an array of objects like the one in the following code example?
public void Test()
{
List arObject = new List();
arObject.Add(Color.Red);
arObject.Add(Color.Green);
arObject.Add(new string("Hello World!"));
arObject.Add(new MyObject());// How to serialize to and from file here?
}In my program the object array represents an array of property values which could be of any time. Custom objects implement the
IXmlSerializable
interface, and simple types like strings appear to work fine. So far I have found problems with the low-level types likeSystem.Drawing.Color
and the like. Is there a generic way of serializing objects which do not implement theIXmlSerializable
interface? At the moment I am not using theXmlSerializer
class because I need extra control over how objects are serialized. So instead I am just using theXmlWriter
class. Any help would be greatly appreciated! Lea HayesHi! Does it have to be XML? Otherwise the
BinaryFormatter
is a very easily to use alternative. Or use theSoapFormatter
(although I think it has a problem with generics IIRC).Regards, mav -- Black holes are the places where God divided by 0...
-
Hi! Does it have to be XML? Otherwise the
BinaryFormatter
is a very easily to use alternative. Or use theSoapFormatter
(although I think it has a problem with generics IIRC).Regards, mav -- Black holes are the places where God divided by 0...
Hi, Unfortunately the
BinaryFormatter
will not do the trick because it is not easily edited by the user. I keep seeingSoapFormatter
pop up here and there, I've not actually use this before so I think it is time to dig into some MSDN documentation and the brilliant selection of articles on CodeProject! Thanks for your advice! Lea Hayes -
Hi, Unfortunately the
BinaryFormatter
will not do the trick because it is not easily edited by the user. I keep seeingSoapFormatter
pop up here and there, I've not actually use this before so I think it is time to dig into some MSDN documentation and the brilliant selection of articles on CodeProject! Thanks for your advice! Lea HayesHi! Yes, the binary files created by
BinaryFormatter
cannot be edited easily by the user, but it's also a way to protect the integrity of your serialized data. When users can play around, they will and then you'll have to deal with invalid XML files... :sigh: So depending on what you want the user to edit, creating a separate function to modify the serialized data could be a safer option.Regards, mav -- Black holes are the places where God divided by 0...