[Mono-list] XmlSerializer trouble

Christian Glodt chris@mind.lu
Thu, 01 Apr 2004 23:46:07 +0200


--=-d7MhbDEE1On0sN6clCFP
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

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


--=-d7MhbDEE1On0sN6clCFP
Content-Disposition: attachment; filename=testcase.cs
Content-Type: text/x-csharp; name=testcase.cs; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit

using System;
using System.Xml.Serialization;
using System.Collections;

class MainClass {

	public static void Main(string[] args) {
	
		Root root = Root.createDefault();
		XmlSerializer ser = new XmlSerializer(typeof(Root));
		ser.Serialize(Console.Out, root);
	}
}

public abstract class TreeNode {

	// Only for serialization
	public TreeNode() {}

	protected Root root;
	
	public Root Root {
		get {
			return root;
		}
	}

	public TreeNode(Root root) {
		this.root = root;
	}
	
	public abstract ArrayList GetChildren();
	
	public abstract void RemoveChild(TreeNode child);
}

public class Root {

	// Only for serialization
	public Root() {}

	private Site site;

	public Site Site {
		set {
			site = value;
		}
		get {
			return site;
		}
	}

	public static Root createDefault() {
		Root root = new Root();
		root.Site = new Site(root);
		root.Site.addSection(new Section(root));
		((Section)root.Site.Sections[0]).addPage(new Page(root));
		((Page)((Section)root.Site.Sections[0]).Pages[0]).addEntry(new Entry(root));
		return root;
	}
}

public class Site : Page {

	// Only for serialization
	public Site() {}

	private ArrayList sections = new ArrayList();
	
	public ArrayList Sections {
		get {
			return sections;
		}		
	}

	public Site(Root root) : base(root) {
	}

	public void addSection(Section s) {
		sections.Add(s);
	}
	
	public void removeSection(Section s) {
		sections.Remove(s);
	}
	
	override public ArrayList GetChildren() {
		ArrayList res = new ArrayList();
		res.AddRange(this.sections);
		res.AddRange(this.Entries);
		return res;
	}

	override public void RemoveChild(TreeNode child) {
		if (this.sections.Contains(child)) this.removeSection((Section)child);
		if (this.Entries.Contains(child)) this.removeEntry((Entry)child);
	}
}

public class Section : Page {

	// Only for serialization
	public Section() {}

	private ArrayList pages = new ArrayList();
	
	public ArrayList Pages {
		get {
			return pages;
		}		
	}

	private ArrayList sections = new ArrayList();
	
	public ArrayList Sections {
		get {
			return sections;
		}
	}

	public Section(Root root) : base(root) {
	}

	public void addPage(Page p) {
		pages.Add(p);
	}
	
	public void removePage(Page p) {
		pages.Remove(p);
	}

	public void addSection(Section s) {
		sections.Add(s);
	}
	
	public void removeSection(Section s) {
		sections.Remove(s);
	}
	
	override public ArrayList GetChildren() {
		ArrayList res = new ArrayList();
		res.AddRange(this.sections);
		res.AddRange(this.pages);
		res.AddRange(this.Entries);
		return res;
	}
	
	override public void RemoveChild(TreeNode child) {
			if (this.sections.Contains(child)) this.removeSection((Section)child);
			if (this.pages.Contains(child)) this.removePage((Page)child);
			if (this.Entries.Contains(child)) this.removeEntry((Entry)child);
	}
}

public class Page : TreeNode {

	// Only for serialization
	public Page() {}

	private ArrayList entries = new ArrayList();
	
	public ArrayList Entries {

		get {
			return entries;
		}		
	}
	
	public Page(Root root) : base(root) {
	}

	public void addEntry(Entry e) {
		entries.Add(e);
	}
	
	public void removeEntry(Entry e) {
		entries.Remove(e);
	}
	
	override public ArrayList GetChildren() {
		return this.entries;
	}

	override public void RemoveChild(TreeNode child) {
			if (this.Entries.Contains(child)) this.removeEntry((Entry)child);
	}
}

public class Entry : TreeNode {

	// Only for serialization
	public Entry() {}

	public Entry(Root root) : base(root) {
	}

	override public ArrayList GetChildren() {
		return new ArrayList();
	}	

	override public void RemoveChild(TreeNode child) {
	}
}

--=-d7MhbDEE1On0sN6clCFP--