[Mono-list] XmlIncludeAttribute missing class

Ricardo Gladwell ricardo.gladwell@btinternet.com
Wed, 11 May 2005 12:49:04 +0100


I'm getting the following exception when I attempt to deserialize some 
objects in my application using System.Xml.Serialization:

System.InvalidOperationException: The type WebPuppy.Model.Site was not 
expected. Use the XmlInclude or SoapInclude attribute to specify types 
that are not known statically.

I've been careful to include all the relevant objects using the 
XmlIncludeAttribute so I can't figure out why the XML serializer is not 
properly finding the Site object. See code fragment below:

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

public class Test {
     public static void Main () {
         Configuration configuration = new Configuration();
         Site site = new Site();
         site.Id = "id";
         configuration.Sites.Add( site );
         XmlSerializer serializer = new XmlSerializer (typeof 
(Configuration));
         using (FileStream fs = new FileStream ("test.xml", 
FileMode.Create)) {
             serializer.Serialize (fs, configuration);
         }
     }
}

public class Configuration {

	Folder sites = new Folder();

	public Folder Sites
	{
		get {
			return sites;
		}
		set {
			this.sites = value;
		}
	}

}

[XmlInclude (typeof (Folder))]
[XmlInclude (typeof (Site))]
public abstract class Artifact {
	public string Id;
}

public class Folder : Artifact, IEnumerable
{
	private Hashtable artifacts = new Hashtable ();

	public IEnumerator GetEnumerator ()
	{
		return artifacts.Values.GetEnumerator ();
	}

	public void Add(object o) {
         Artifact artifact = (Artifact) o;
		artifacts[artifact.Id] = (Artifact) artifact;
	}
}

public class Site : Folder
{
}