[Mono-devel-list] XmlSerializer design problems
Atsushi Eno
atsushi at ximian.com
Tue Aug 24 04:05:05 EDT 2004
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