mail-archives/mono-list/2003-October/016399.html

139 строки
6.0 KiB
HTML
Исходник Постоянная ссылка Ответственный История

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE> [Mono-list] Xml Deserialize + Constructor Needed
</TITLE>
<LINK REL="Index" HREF="index.html" >
<LINK REL="made" HREF="mailto:rafaelteixeirabr%40hotmail.com">
<META NAME="robots" CONTENT="index,nofollow">
<LINK REL="Previous" HREF="016396.html">
<LINK REL="Next" HREF="016426.html">
</HEAD>
<BODY BGCOLOR="#ffffff">
<H1>[Mono-list] Xml Deserialize + Constructor Needed
</H1>
<B>A Rafael D Teixeira
</B>
<A HREF="mailto:rafaelteixeirabr%40hotmail.com"
TITLE="[Mono-list] Xml Deserialize + Constructor Needed">rafaelteixeirabr@hotmail.com
</A><BR>
<I>Tue, 14 Oct 2003 12:18:12 -0200</I>
<P><UL>
<LI> Previous message: <A HREF="016396.html">[Mono-list] mono blogs
</A></li>
<LI> Next message: <A HREF="016426.html">[Mono-list] Can I use my application on LInux?
</A></li>
<LI> <B>Messages sorted by:</B>
<a href="date.html#16399">[ date ]</a>
<a href="thread.html#16399">[ thread ]</a>
<a href="subject.html#16399">[ subject ]</a>
<a href="author.html#16399">[ author ]</a>
</LI>
</UL>
<HR>
<!--beginarticle-->
<PRE>Well, it's a necessity for xml serialization (I learned it the hard way),
that you have an &quot;Default Constructor&quot;, what means that you need a
parameterless constructor (besides your other constructors).
That is easy to understand why: when deserializing the xml, the deserializer
sees an element that maps to a class, it then needs to create an instance of
that class, an *empty* instance of the class, that later will be &quot;fleshed&quot;
with other elements and attributes values (that's is also why only public
fields/properties can be serialized, because the serializer/deserializer is
outside the class).
If you furnish only constructors with parameters, how is the serializer
going to find what values, should it be passing to your constructor?
Best regards,
Rafael Teixeira
Brazilian Polymath
Mono Hacker since 16 Jul 2001
English Blog: <A HREF="http://monoblog.blogspot.com/">http://monoblog.blogspot.com/</A>
Brazilian Portuguese Blog: <A HREF="http://monoblog.weblogger.terra.com.br/">http://monoblog.weblogger.terra.com.br/</A>
&gt;<i>From: Pablo Fischer &lt;<A HREF="mailto:pablo@pablo.com.mx">pablo@pablo.com.mx</A>&gt;
</I>&gt;<i>To: <A HREF="mailto:mono-list@lists.ximian.com">mono-list@lists.ximian.com</A>
</I>&gt;<i>Subject: [Mono-list] Xml Deserialize + Constructor Needed
</I>&gt;<i>Date: Mon, 13 Oct 2003 23:58:22 -0500
</I>&gt;<i>
</I>&gt;<i>Hi all!
</I>&gt;<i>
</I>&gt;<i>I created a file in XML with the help of XmlSerialization, how I want to
</I>&gt;<i>Deserialize it, I found in may sites (also in msdn) a simple way to do
</I>&gt;<i>this, so my method:
</I>&gt;<i>
</I>&gt;<i>private static void Desarmar() {
</I>&gt;<i> Accounts accs = new Accounts(&quot;test&quot;);
</I>&gt;<i> XmlSerialize mySerializer = new XmlSerializer(typeof(Accounts));
</I>&gt;<i> FileStream myFileStream = new FileStream(&quot;.mbloggyrc&quot;, FileMode.Open);
</I>&gt;<i> accs = (Accounts)mySerializer.Deserialize(myFileStream);
</I>&gt;<i>}
</I>&gt;<i>
</I>&gt;<i>It compiles ok, but when I run it I get:
</I>&gt;<i>
</I>&gt;<i>Unhandled Exception: System.MissingMethodException: Default constructor
</I>&gt;<i>not found
</I>&gt;<i>in &lt;0x00144&gt; System.Activator:CreateInstance (System.Type,bool)
</I>&gt;<i>in &lt;0x00012&gt; System.Activator:CreateInstance (System.Type)
</I>&gt;<i>in &lt;0x001ab&gt;
</I>&gt;<i>System.Xml.Serialization.XmlSerializationReaderInterpreter:ReadClassInstance
</I>&gt;<i>(System.Xml.Serialization.XmlTypeMapping,bool,bool)
</I>&gt;<i>in &lt;0x00067&gt;
</I>&gt;<i>System.Xml.Serialization.XmlSerializationReaderInterpreter:ReadObject
</I>&gt;<i>(System.Xml.Serialization.XmlTypeMapping,bool,bool)
</I>&gt;<i>in &lt;0x000a2&gt;
</I>&gt;<i>System.Xml.Serialization.XmlSerializationReaderInterpreter:ReadObject ()
</I>&gt;<i>in &lt;0x00075&gt; System.Xml.Serialization.XmlSerializer:Deserialize
</I>&gt;<i>(System.Xml.Serialization.XmlSerializationReader)
</I>&gt;<i>in &lt;0x00068&gt; System.Xml.Serialization.XmlSerializer:Deserialize
</I>&gt;<i>(System.Xml.XmlReader)
</I>&gt;<i>in &lt;0x0003f&gt; System.Xml.Serialization.XmlSerializer:Deserialize
</I>&gt;<i>(System.IO.Stream)
</I>&gt;<i>in &lt;0x0009a&gt; .testing:Desarmar ()
</I>&gt;<i>in &lt;0x0012c&gt; .testing:Main ()
</I>&gt;<i>
</I>&gt;<i>A Default Constructor.. for what? I have a Constructor for each
</I>&gt;<i>element..
</I>&gt;<i>
</I>&gt;<i>Thanks!
</I>&gt;<i>Pablo
</I>&gt;<i>--
</I>&gt;<i>Pablo Fischer Sandoval (pablo [arroba/at] pablo.com.mx)
</I>&gt;<i><A HREF="http://www.pablo.com.mx">http://www.pablo.com.mx</A>
</I>&gt;<i><A HREF="http://www.debianmexico.org">http://www.debianmexico.org</A>
</I>&gt;<i>GPG FingerTip: 3D49 4CB8 8951 F2CA 8131 AF7C D1B9 1FB9 6B11 810C
</I>&gt;<i>Firma URL: <A HREF="http://www.pablo.com.mx/firmagpg.txt">http://www.pablo.com.mx/firmagpg.txt</A>
</I>&gt;<i>
</I>&gt;<i>_______________________________________________
</I>&gt;<i>Mono-list maillist - <A HREF="mailto:Mono-list@lists.ximian.com">Mono-list@lists.ximian.com</A>
</I>&gt;<i><A HREF="http://lists.ximian.com/mailman/listinfo/mono-list">http://lists.ximian.com/mailman/listinfo/mono-list</A>
</I>
_________________________________________________________________
MSN Messenger: instale gr<67>tis e converse com seus amigos.
<A HREF="http://messenger.msn.com.br">http://messenger.msn.com.br</A>
</PRE>
<!--endarticle-->
<HR>
<P><UL>
<!--threads-->
<LI> Previous message: <A HREF="016396.html">[Mono-list] mono blogs
</A></li>
<LI> Next message: <A HREF="016426.html">[Mono-list] Can I use my application on LInux?
</A></li>
<LI> <B>Messages sorted by:</B>
<a href="date.html#16399">[ date ]</a>
<a href="thread.html#16399">[ thread ]</a>
<a href="subject.html#16399">[ subject ]</a>
<a href="author.html#16399">[ author ]</a>
</LI>
</UL>
</body></html>