[Mono-list] System.Xml.Serialization.XmlSerializer
Urs Muff
umuff@quark.com
Thu, 30 Jan 2003 10:28:05 -0700
This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.
------_=_NextPart_001_01C2C884.F2C7BDD0
Content-Type: text/plain
Does the mono implementation of System.Xml.Serialization.XmlSerializer allow
methods or properties to use interfaces? An interface and an abstract class
should work exchangeable but the Microsoft runtime does not allow interfaces
even when using the XmlElement(Type = typeof(someType)) attribute on that
property.
Sample code:
public interface ISample { string val { get; } }
public abstract class ASample : ISample
{
public abstract string val { get; set; }
}
public class S : ASample
{
string v;
public S() : this("") {}
public S(string s) { v = s; }
public override string val { get { return v; } set { v =
value; } }
}
public class Sample
{
string a, b;
[XmlElement(typeof(S))]
public ASample A { get { return new S(a); } set { a =
value.val; } }
[/*XmlIgnore(),*/XmlElement(typeof(S))]
public ISample B { get { return new S(b); } set { b =
value.val; } }
public Sample() { a = "a value"; b = "b value"; }
}
public class XmlSerialization
{
static public string ToString(object o)
{
System.IO.StringWriter w = new
System.IO.StringWriter();
try
{
XmlSerializer serializer = new
XmlSerializer(typeof(Sample));
serializer.Serialize(w,new Sample());
}
catch (System.Exception e)
{
System.Exception ie = e.InnerException;
while (ie != null)
{
System.Diagnostics.Debug.WriteLine(ie.ToString());
ie = ie.InnerException;
}
}
w.Close();
return w.ToString();
}
}
If I put comment out XmlIgnore I get the following output:
<?xml version="1.0" encoding="utf-16"?>
<Sample xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<A>
<val>a value</val>
</A>
</Sample>
- URS C. MUFF
------_=_NextPart_001_01C2C884.F2C7BDD0
Content-Type: text/html
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV=3D"Content-Type" CONTENT=3D"text/html; =
charset=3DUS-ASCII">
<META NAME=3D"Generator" CONTENT=3D"MS Exchange Server version =
5.5.2653.12">
<TITLE>System.Xml.Serialization.XmlSerializer</TITLE>
</HEAD>
<BODY>
<P ALIGN=3DLEFT><FONT SIZE=3D2 FACE=3D"Courier New">Does the mono =
implementation of</FONT> <FONT SIZE=3D2 FACE=3D"Courier =
New">System.Xml.Serialization.XmlSerializer</FONT><FONT SIZE=3D2 =
FACE=3D"Courier New"> allow methods or properties to use =
interfaces? An interface and an abstract class should work</FONT> =
<FONT SIZE=3D2 FACE=3D"Courier New">exchangeable but the Microsoft =
runtime does not allow interfaces even when using the XmlElement(Type =
=3D typeof(someType)) attribute on that property.</FONT></P>
<P ALIGN=3DLEFT><FONT SIZE=3D2 FACE=3D"Courier New">Sample</FONT> <FONT =
SIZE=3D2 FACE=3D"Courier New">code:</FONT></P>
<P ALIGN=3DLEFT> <FONT =
COLOR=3D"#0000FF" SIZE=3D2 FACE=3D"Courier New">public</FONT><FONT =
SIZE=3D2 FACE=3D"Courier New"></FONT> <FONT COLOR=3D"#0000FF" SIZE=3D2 =
FACE=3D"Courier New">interface</FONT><FONT SIZE=3D2 FACE=3D"Courier =
New"> ISample {</FONT> <FONT COLOR=3D"#0000FF" SIZE=3D2 FACE=3D"Courier =
New">string</FONT><FONT SIZE=3D2 FACE=3D"Courier New"> val {</FONT> =
<FONT COLOR=3D"#0000FF" SIZE=3D2 FACE=3D"Courier New">get</FONT><FONT =
SIZE=3D2 FACE=3D"Courier New">; } }</FONT></P>
<P ALIGN=3DLEFT> <FONT =
COLOR=3D"#0000FF" SIZE=3D2 FACE=3D"Courier New">public</FONT><FONT =
SIZE=3D2 FACE=3D"Courier New"></FONT> <FONT COLOR=3D"#0000FF" SIZE=3D2 =
FACE=3D"Courier New">abstract</FONT><FONT SIZE=3D2 FACE=3D"Courier =
New"></FONT> <FONT COLOR=3D"#0000FF" SIZE=3D2 FACE=3D"Courier =
New">class</FONT><FONT SIZE=3D2 FACE=3D"Courier New"> ASample : =
ISample</FONT></P>
<P ALIGN=3DLEFT> <FONT =
SIZE=3D2 FACE=3D"Courier New">{</FONT></P>
<P ALIGN=3DLEFT> =
<FONT COLOR=3D"#0000FF" =
SIZE=3D2 FACE=3D"Courier New">public</FONT><FONT SIZE=3D2 =
FACE=3D"Courier New"></FONT> <FONT COLOR=3D"#0000FF" SIZE=3D2 =
FACE=3D"Courier New">abstract</FONT><FONT SIZE=3D2 FACE=3D"Courier =
New"></FONT> <FONT COLOR=3D"#0000FF" SIZE=3D2 FACE=3D"Courier =
New">string</FONT><FONT SIZE=3D2 FACE=3D"Courier New"> val {</FONT> =
<FONT COLOR=3D"#0000FF" SIZE=3D2 FACE=3D"Courier New">get</FONT><FONT =
SIZE=3D2 FACE=3D"Courier New">;</FONT> <FONT COLOR=3D"#0000FF" SIZE=3D2 =
FACE=3D"Courier New">set</FONT><FONT SIZE=3D2 FACE=3D"Courier New">; =
}</FONT></P>
<P ALIGN=3DLEFT> <FONT =
SIZE=3D2 FACE=3D"Courier New">}</FONT></P>
<P ALIGN=3DLEFT> <FONT =
COLOR=3D"#0000FF" SIZE=3D2 FACE=3D"Courier New">public</FONT><FONT =
SIZE=3D2 FACE=3D"Courier New"></FONT> <FONT COLOR=3D"#0000FF" SIZE=3D2 =
FACE=3D"Courier New">class</FONT><FONT SIZE=3D2 FACE=3D"Courier New"> S =
: ASample</FONT></P>
<P ALIGN=3DLEFT> <FONT =
SIZE=3D2 FACE=3D"Courier New">{</FONT></P>
<P ALIGN=3DLEFT> =
<FONT COLOR=3D"#0000FF" =
SIZE=3D2 FACE=3D"Courier New">string</FONT><FONT SIZE=3D2 =
FACE=3D"Courier New"> v;</FONT></P>
<P ALIGN=3DLEFT> =
<FONT COLOR=3D"#0000FF" =
SIZE=3D2 FACE=3D"Courier New">public</FONT><FONT SIZE=3D2 =
FACE=3D"Courier New"> S() :</FONT> <FONT COLOR=3D"#0000FF" SIZE=3D2 =
FACE=3D"Courier New">this</FONT><FONT SIZE=3D2 FACE=3D"Courier =
New">("") {}</FONT></P>
<P ALIGN=3DLEFT> =
<FONT COLOR=3D"#0000FF" =
SIZE=3D2 FACE=3D"Courier New">public</FONT><FONT SIZE=3D2 =
FACE=3D"Courier New"> S(</FONT><FONT COLOR=3D"#0000FF" SIZE=3D2 =
FACE=3D"Courier New">string</FONT><FONT SIZE=3D2 FACE=3D"Courier New"> =
s) { v =3D s; }</FONT></P>
<P ALIGN=3DLEFT> =
<FONT COLOR=3D"#0000FF" =
SIZE=3D2 FACE=3D"Courier New">public</FONT><FONT SIZE=3D2 =
FACE=3D"Courier New"></FONT> <FONT COLOR=3D"#0000FF" SIZE=3D2 =
FACE=3D"Courier New">override</FONT><FONT SIZE=3D2 FACE=3D"Courier =
New"></FONT> <FONT COLOR=3D"#0000FF" SIZE=3D2 FACE=3D"Courier =
New">string</FONT><FONT SIZE=3D2 FACE=3D"Courier New"> val {</FONT> =
<FONT COLOR=3D"#0000FF" SIZE=3D2 FACE=3D"Courier New">get</FONT><FONT =
SIZE=3D2 FACE=3D"Courier New"> {</FONT> <FONT COLOR=3D"#0000FF" =
SIZE=3D2 FACE=3D"Courier New">return</FONT><FONT SIZE=3D2 =
FACE=3D"Courier New"> v; }</FONT> <FONT COLOR=3D"#0000FF" SIZE=3D2 =
FACE=3D"Courier New">set</FONT><FONT SIZE=3D2 FACE=3D"Courier New"> { v =
=3D</FONT> <FONT COLOR=3D"#0000FF" SIZE=3D2 FACE=3D"Courier =
New">value</FONT><FONT SIZE=3D2 FACE=3D"Courier New">; } }</FONT></P>
<P ALIGN=3DLEFT> <FONT =
SIZE=3D2 FACE=3D"Courier New">}</FONT></P>
<P ALIGN=3DLEFT> <FONT =
COLOR=3D"#0000FF" SIZE=3D2 FACE=3D"Courier New">public</FONT><FONT =
SIZE=3D2 FACE=3D"Courier New"></FONT> <FONT COLOR=3D"#0000FF" SIZE=3D2 =
FACE=3D"Courier New">class</FONT><FONT SIZE=3D2 FACE=3D"Courier New"> =
Sample</FONT></P>
<P ALIGN=3DLEFT> <FONT =
SIZE=3D2 FACE=3D"Courier New">{</FONT></P>
<P ALIGN=3DLEFT> =
<FONT COLOR=3D"#0000FF" =
SIZE=3D2 FACE=3D"Courier New">string</FONT><FONT SIZE=3D2 =
FACE=3D"Courier New"> a, b;</FONT></P>
<P ALIGN=3DLEFT> =
<FONT SIZE=3D2 =
FACE=3D"Courier New">[XmlElement(</FONT><FONT COLOR=3D"#0000FF" =
SIZE=3D2 FACE=3D"Courier New">typeof</FONT><FONT SIZE=3D2 =
FACE=3D"Courier New">(S))]</FONT></P>
<P ALIGN=3DLEFT> =
<FONT COLOR=3D"#0000FF" =
SIZE=3D2 FACE=3D"Courier New">public</FONT><FONT SIZE=3D2 =
FACE=3D"Courier New"> ASample A {</FONT> <FONT COLOR=3D"#0000FF" =
SIZE=3D2 FACE=3D"Courier New">get</FONT><FONT SIZE=3D2 FACE=3D"Courier =
New"> {</FONT> <FONT COLOR=3D"#0000FF" SIZE=3D2 FACE=3D"Courier =
New">return</FONT><FONT SIZE=3D2 FACE=3D"Courier New"></FONT> <FONT =
COLOR=3D"#0000FF" SIZE=3D2 FACE=3D"Courier New">new</FONT><FONT =
SIZE=3D2 FACE=3D"Courier New"> S(a); }</FONT> <FONT COLOR=3D"#0000FF" =
SIZE=3D2 FACE=3D"Courier New">set</FONT><FONT SIZE=3D2 FACE=3D"Courier =
New"> { a =3D</FONT> <FONT COLOR=3D"#0000FF" SIZE=3D2 FACE=3D"Courier =
New">value</FONT><FONT SIZE=3D2 FACE=3D"Courier New">.val; } =
}</FONT></P>
<P ALIGN=3DLEFT> =
</P>
<P ALIGN=3DLEFT> =
<FONT SIZE=3D2 =
FACE=3D"Courier New">[</FONT><FONT COLOR=3D"#008000" SIZE=3D2 =
FACE=3D"Courier New">/*XmlIgnore(),*/</FONT><FONT SIZE=3D2 =
FACE=3D"Courier New">XmlElement(</FONT><FONT COLOR=3D"#0000FF" SIZE=3D2 =
FACE=3D"Courier New">typeof</FONT><FONT SIZE=3D2 FACE=3D"Courier =
New">(S))]</FONT></P>
<P ALIGN=3DLEFT> =
<FONT COLOR=3D"#0000FF" =
SIZE=3D2 FACE=3D"Courier New">public</FONT><FONT SIZE=3D2 =
FACE=3D"Courier New"> ISample B {</FONT> <FONT COLOR=3D"#0000FF" =
SIZE=3D2 FACE=3D"Courier New">get</FONT><FONT SIZE=3D2 FACE=3D"Courier =
New"> {</FONT> <FONT COLOR=3D"#0000FF" SIZE=3D2 FACE=3D"Courier =
New">return</FONT><FONT SIZE=3D2 FACE=3D"Courier New"></FONT> <FONT =
COLOR=3D"#0000FF" SIZE=3D2 FACE=3D"Courier New">new</FONT><FONT =
SIZE=3D2 FACE=3D"Courier New"> S(b); }</FONT> <FONT COLOR=3D"#0000FF" =
SIZE=3D2 FACE=3D"Courier New">set</FONT><FONT SIZE=3D2 FACE=3D"Courier =
New"> { b =3D</FONT> <FONT COLOR=3D"#0000FF" SIZE=3D2 FACE=3D"Courier =
New">value</FONT><FONT SIZE=3D2 FACE=3D"Courier New">.val; } =
}</FONT></P>
<P ALIGN=3DLEFT> =
<FONT COLOR=3D"#0000FF" =
SIZE=3D2 FACE=3D"Courier New">public</FONT><FONT SIZE=3D2 =
FACE=3D"Courier New"> Sample() { a =3D "a value"; b =3D =
"b value"; }</FONT></P>
<P ALIGN=3DLEFT> <FONT =
SIZE=3D2 FACE=3D"Courier New">}</FONT></P>
<P ALIGN=3DLEFT> <FONT =
COLOR=3D"#0000FF" SIZE=3D2 FACE=3D"Courier New">public</FONT><FONT =
SIZE=3D2 FACE=3D"Courier New"></FONT> <FONT COLOR=3D"#0000FF" SIZE=3D2 =
FACE=3D"Courier New">class</FONT><FONT SIZE=3D2 FACE=3D"Courier New"> =
XmlSerialization</FONT></P>
<P ALIGN=3DLEFT> <FONT =
SIZE=3D2 FACE=3D"Courier New">{</FONT></P>
<P ALIGN=3DLEFT> =
<FONT COLOR=3D"#0000FF" =
SIZE=3D2 FACE=3D"Courier New">static</FONT><FONT SIZE=3D2 =
FACE=3D"Courier New"></FONT> <FONT COLOR=3D"#0000FF" SIZE=3D2 =
FACE=3D"Courier New">public</FONT><FONT SIZE=3D2 FACE=3D"Courier =
New"></FONT> <FONT COLOR=3D"#0000FF" SIZE=3D2 FACE=3D"Courier =
New">string</FONT><FONT SIZE=3D2 FACE=3D"Courier New"> =
ToString(</FONT><FONT COLOR=3D"#0000FF" SIZE=3D2 FACE=3D"Courier =
New">object</FONT><FONT SIZE=3D2 FACE=3D"Courier New"> o)</FONT></P>
<P ALIGN=3DLEFT> =
<FONT SIZE=3D2 =
FACE=3D"Courier New">{</FONT></P>
<P ALIGN=3DLEFT> =
=
<FONT SIZE=3D2 =
FACE=3D"Courier New">System.IO.StringWriter w =3D</FONT> <FONT =
COLOR=3D"#0000FF" SIZE=3D2 FACE=3D"Courier New">new</FONT><FONT =
SIZE=3D2 FACE=3D"Courier New"> System.IO.StringWriter();</FONT></P>
<P ALIGN=3DLEFT> =
=
</P>
<P ALIGN=3DLEFT> =
=
<FONT COLOR=3D"#0000FF" =
SIZE=3D2 FACE=3D"Courier New">try</FONT><FONT SIZE=3D2 FACE=3D"Courier =
New"> </FONT></P>
<P ALIGN=3DLEFT> =
=
<FONT SIZE=3D2 =
FACE=3D"Courier New">{</FONT></P>
<P ALIGN=3DLEFT> =
=
=
<FONT SIZE=3D2 =
FACE=3D"Courier New">XmlSerializer serializer =3D</FONT> <FONT =
COLOR=3D"#0000FF" SIZE=3D2 FACE=3D"Courier New">new</FONT><FONT =
SIZE=3D2 FACE=3D"Courier New"> XmlSerializer(</FONT><FONT =
COLOR=3D"#0000FF" SIZE=3D2 FACE=3D"Courier New">typeof</FONT><FONT =
SIZE=3D2 FACE=3D"Courier New">(Sample));</FONT></P>
<P ALIGN=3DLEFT> =
=
=
<FONT SIZE=3D2 =
FACE=3D"Courier New">serializer.Serialize(w,</FONT><FONT =
COLOR=3D"#0000FF" SIZE=3D2 FACE=3D"Courier New">new</FONT><FONT =
SIZE=3D2 FACE=3D"Courier New"> Sample());</FONT></P>
<P ALIGN=3DLEFT> =
=
<FONT SIZE=3D2 =
FACE=3D"Courier New">}</FONT></P>
<P ALIGN=3DLEFT> =
=
<FONT COLOR=3D"#0000FF" =
SIZE=3D2 FACE=3D"Courier New">catch</FONT><FONT SIZE=3D2 =
FACE=3D"Courier New"> (System.Exception e) </FONT></P>
<P ALIGN=3DLEFT> =
=
<FONT SIZE=3D2 =
FACE=3D"Courier New">{</FONT></P>
<P ALIGN=3DLEFT> =
=
=
<FONT SIZE=3D2 =
FACE=3D"Courier New">System.Exception ie =3D =
e.InnerException;</FONT></P>
<P ALIGN=3DLEFT> =
=
=
<FONT COLOR=3D"#0000FF" =
SIZE=3D2 FACE=3D"Courier New">while</FONT><FONT SIZE=3D2 =
FACE=3D"Courier New"> (ie !=3D</FONT> <FONT COLOR=3D"#0000FF" SIZE=3D2 =
FACE=3D"Courier New">null</FONT><FONT SIZE=3D2 FACE=3D"Courier New">) =
</FONT></P>
<P ALIGN=3DLEFT> =
=
=
<FONT SIZE=3D2 =
FACE=3D"Courier New">{</FONT></P>
<P ALIGN=3DLEFT> =
=
=
=
<FONT SIZE=3D2 =
FACE=3D"Courier =
New">System.Diagnostics.Debug.WriteLine(ie.ToString());</FONT></P>
<P ALIGN=3DLEFT> =
=
=
=
<FONT SIZE=3D2 =
FACE=3D"Courier New">ie =3D ie.InnerException;</FONT></P>
<P ALIGN=3DLEFT> =
=
=
<FONT SIZE=3D2 =
FACE=3D"Courier New">}</FONT></P>
<P ALIGN=3DLEFT> =
=
<FONT SIZE=3D2 =
FACE=3D"Courier New">}</FONT></P>
<P ALIGN=3DLEFT> =
=
<FONT SIZE=3D2 =
FACE=3D"Courier New">w.Close();</FONT></P>
<P ALIGN=3DLEFT> =
=
<FONT COLOR=3D"#0000FF" =
SIZE=3D2 FACE=3D"Courier New">return</FONT><FONT SIZE=3D2 =
FACE=3D"Courier New"> w.ToString();</FONT></P>
<P ALIGN=3DLEFT> =
<FONT SIZE=3D2 =
FACE=3D"Courier New">}</FONT></P>
<P ALIGN=3DLEFT> <FONT =
SIZE=3D2 FACE=3D"Courier New">}</FONT></P>
<P ALIGN=3DLEFT><FONT SIZE=3D2 FACE=3D"Courier New">If I put</FONT> =
<FONT SIZE=3D2 FACE=3D"Courier New">comment out XmlIgnore I get the =
following output:</FONT></P>
<P ALIGN=3DLEFT><FONT SIZE=3D1 FACE=3D"Courier New"><?xml =
version=3D"1.0" encoding=3D"utf-16"?></FONT></P>
<P ALIGN=3DLEFT><FONT SIZE=3D1 FACE=3D"Courier New"><Sample =
xmlns:xsd=3D"<A HREF=3D"http://www.w3.org/2001/XMLSchema" =
TARGET=3D"_blank">http://www.w3.org/2001/XMLSchema</A>" =
xmlns:xsi=3D"<A HREF=3D"http://www.w3.org/2001/XMLSchema-instance" =
TARGET=3D"_blank">http://www.w3.org/2001/XMLSchema-instance</A>">=
;</FONT></P>
<P ALIGN=3DLEFT><FONT SIZE=3D1 FACE=3D"Courier New"> =
<A></FONT></P>
<P ALIGN=3DLEFT><FONT SIZE=3D1 FACE=3D"Courier New"> =
<val>a value</val></FONT></P>
<P ALIGN=3DLEFT><FONT SIZE=3D1 FACE=3D"Courier New"> =
</A></FONT></P>
<P ALIGN=3DLEFT><FONT SIZE=3D1 FACE=3D"Courier =
New"></Sample></FONT></P>
<P ALIGN=3DLEFT><A NAME=3D"_MailAutoSig"><FONT SIZE=3D2 FACE=3D"Courier =
New">- URS C. MUFF</FONT></A></P>
<P ALIGN=3DLEFT></P>
</BODY>
</HTML>
------_=_NextPart_001_01C2C884.F2C7BDD0--