[Mono-devel-list] XmlSerializer design problems

psonek2 psonek2 at seznam.cz
Tue Aug 24 01:45:35 EDT 2004


Hi,

I was working with XmlSerializer in my projects and here are my problems,
that are not related with Mono, but with XmlSerializer design in general.
I would like to hear some opinions or comments or even solution :-)

XmlSerializer is intended to store object state. But how can I use it to
code simple object with methods Load and Save?

public class Class1 {
	public string Hello = "hello";

	public void Save(Stream s)
	{		
		XmlSerializer serializer = new
XmlSerializer(typeof(Class1));
		FileStream fs = File.OpenWrite("Class1.xml");
		serializer.Serialize(fs, this);
		fs.Close();
	}

	public void Load(Stream s)
	{
		// how to write load method???
	}
}

Save is no problem, but I have no idea how to write Load method.
Serializer.Deserialize just returns new instance. That means you always need
some object "above" to create instances or static method.

This is problem because I can't use serializer to implement for example some
IPersistent interface with Load and Save methods.

I think it's also really bad idea that serializer always creates new
instances in Deserialize method. It would be sometimes enough just to fill
object fields. For example I would like to do:


	public void Load(Stream s)
	{
		XmlSerializer serializer = new
XmlSerializer(typeof(Class1));
		FileStream fs = File.OpenRead("Class1.xml");
		serializer.Deserialize(fs, this);
		fs.Close();		
	}

My question: is there a way how to force XmlSerializer behave the way above?
Maybe I could extend XmlSerializer class, but I haven't found easy way how
to do it.

Regards

Radek






More information about the Mono-devel-list mailing list