[Mono-list] XmlSerializer trouble
Lluis Sanchez
lluis@ximian.com
Sat, 03 Apr 2004 16:34:58 +0200
Hi,
In the declaration of the Sections property, add an XmlArrayItem
attribute specifying the type of the items, like this:
[XmlArrayItem(typeof(Section))]
public ArrayList Sections {
get {
return sections;
}
}
You need to do this because the xml serializer cannot infer the type of
the items from ArrayList.
Lluis.
On dj, 2004-04-01 at 23:46, Christian Glodt wrote:
> Hi,
>
> in a program that I am currently developing, I am encountering
> some problems with xml serialization. I am trying to serialize the
> application's model (which has some inheritance) by passing the
> root of the model's object graph to the serializer. A reduced version
> of the code which demonstrates the problem is attached.
>
> I get the following output:
>
> <?xml version="1.0" encoding="iso-8859-1"?>
> <Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xmlns:xsd="http://www.w3.org/2001/XMLSchema">
> <Site>
> <Entries />
> <Sections>
> <anyType
> Unhandled Exception: System.InvalidOperationException: Invalid type:
> Section
> in <0x00142>
> System.Xml.Serialization.XmlSerializationWriter:WriteTypedPrimitive
> (string,string,object,bool)
> in <0x0027f>
> System.Xml.Serialization.XmlSerializationWriterInterpreter:WriteObject
> (System.Xml.Serialization.XmlTypeMapping,object,string,string,bool,bool,bool)
> in <0x0099b>
> System.Xml.Serialization.XmlSerializationWriterInterpreter:WriteMemberElement (System.Xml.Serialization.XmlTypeMapElementInfo,object)
> in <0x003bb>
> System.Xml.Serialization.XmlSerializationWriterInterpreter:WriteListContent (System.Xml.Serialization.TypeData,System.Xml.Serialization.ListMap,object,System.Text.StringBuilder)
> in <0x00750>
> System.Xml.Serialization.XmlSerializationWriterInterpreter:WriteMemberElement (System.Xml.Serialization.XmlTypeMapElementInfo,object)
> in <0x0017c>
> System.Xml.Serialization.XmlSerializationWriterInterpreter:WriteElementMembers (System.Xml.Serialization.ClassMap,object,bool)
> in <0x00056>
> System.Xml.Serialization.XmlSerializationWriterInterpreter:WriteObjectElementElements (System.Xml.Serialization.XmlTypeMapping,object)
> in <0x000d3>
> System.Xml.Serialization.XmlSerializationWriterInterpreter:WriteObjectElement (System.Xml.Serialization.XmlTypeMapping,object,string,string)
> in <0x0035c>
> System.Xml.Serialization.XmlSerializationWriterInterpreter:WriteObject
> (System.Xml.Serialization.XmlTypeMapping,object,string,string,bool,bool,bool)
> in <0x0099b>
> System.Xml.Serialization.XmlSerializationWriterInterpreter:WriteMemberElement (System.Xml.Serialization.XmlTypeMapElementInfo,object)
> in <0x002a3>
> System.Xml.Serialization.XmlSerializationWriterInterpreter:WriteElementMembers (System.Xml.Serialization.ClassMap,object,bool)
> in <0x00056>
> System.Xml.Serialization.XmlSerializationWriterInterpreter:WriteObjectElementElements (System.Xml.Serialization.XmlTypeMapping,object)
> in <0x000d3>
> System.Xml.Serialization.XmlSerializationWriterInterpreter:WriteObjectElement (System.Xml.Serialization.XmlTypeMapping,object,string,string)
> in <0x0035c>
> System.Xml.Serialization.XmlSerializationWriterInterpreter:WriteObject
> (System.Xml.Serialization.XmlTypeMapping,object,string,string,bool,bool,bool)
> in <0x0013a>
> System.Xml.Serialization.XmlSerializationWriterInterpreter:WriteRoot
> (object)
> in <0x00095> System.Xml.Serialization.XmlSerializer:Serialize
> (object,System.Xml.Serialization.XmlSerializationWriter)
> in <0x000db> System.Xml.Serialization.XmlSerializer:Serialize
> (System.Xml.XmlWriter,object,System.Xml.Serialization.XmlSerializerNamespaces)
> in <0x0005b> System.Xml.Serialization.XmlSerializer:Serialize
> (System.IO.TextWriter,object)
> in <0x00050> MainClass:Main (string[])
>
>
> I wonder if I am making some mistake, or if there is a problem with
> serialization. As far as I can see from MSDN docs this should just work.
> Also, in java this kind of thing just works (although not with xml).
>
> I appreciate any kind of help - this is my first C#/mono program.
> Mono/mcs is version 0.31 on Debian unstable. I had the same problem
> with 0.30.2.
>
> Please CC me in your replies. I'm not subscribed but I follow the
> archives.
>
> Thanks & Cheers,
>
> Christian Glodt
>