[Mono-devel-list] XmlSerializer design problems

Radek Polak psonek2 at seznam.cz
Tue Aug 24 07:47:19 EDT 2004


Thanks for interest,
such wrapper seems to be a good solution, but it was also
source of my problems. If i want wrapper to work for me, it must
be based on interfaces and they are not serializable as 
serializer is implemented now.

But if we implemented serializer the way i suggested it would be
possible to serialize just the interface - not the whole object.
I need to do something like:

public interface IHello
{
	string Hello {get;}
}

public class Class1 : IHello
{
	string hello = "hello";

	string IHello.Hello
	{
		get	{ return hello; }
	}

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

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

Note: new XmlSerializer(typeof(IHello)) does not work normally.

Maybe you dont see my point yet, but in my real application i ran
into situation when serializer is unusable, though if i wrote XML
manually or had serializer that could just "fill" the properties 
and that could serialize also interfaces, it would work perfectly
for me.

I can send also my real application or the model, if needed.

Regards

Radek



-----Original Message-----
From: Atsushi Eno [mailto:atsushi at ximian.com] 
Sent: Tuesday, August 24, 2004 9:05 AM
To: psonek2
Cc: mono-devel-list at lists.ximian.com
Subject: Re: [Mono-devel-list] XmlSerializer design problems

Hello,

One thing I'd point out is that you are going to require any of
developers who develop XML serializable classes to make those
classes consistent with such deserialization that might happen
at any state of the instance.

Why don't just create a wrapper object that wraps xml serializable
object?

public class PersistentWrapper
{
   XmlSerializableFoo foo;
   public void Save (Stream s) {
     GetSerializer ().Serialize (s, foo);
   }
   public void Load (Stream s) {
     foo = (XmlSerializableFoo) GetSerializer ().Deserialize (s);
   }
   XmlSerializer GetSerializer () {
     return new XmlSerializer (typeof (XmlSerializableFoo));
   }
}

XmlSerializer is nice tool for automated xml processing, but
there are many classes that could not stand with XmlSerializer
limitations. For example, XmlSerializer cannot deserialize
System.Xml.XmlSchema (that's why there is XmlSchema.Read()).


Atsushi Eno


psonek2 wrote:
> 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
> 
> 
> 
> _______________________________________________
> Mono-devel-list mailing list
> Mono-devel-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-devel-list
> 





More information about the Mono-devel-list mailing list