[Mono-list] XmlIncludeAttribute missing class

Lluis Sanchez lluis@ximian.com
Wed, 11 May 2005 18:43:57 +0200


Hi,

When building the xml map, the XmlSerializer detects that the Folder
class is a collection (because it implements IEnumerable and has an Add
method). Collections are not serialized like regular data classes. For
example, public members of collections are ignored, only the collection
items are serialized. For the same reason, the XmlSerializer ignores the
inheritance hierarchy of the collection. This means that it will ignore
the Artifact class and the XmlInclude attributes it has.

This behavior is "by design" and you will get the same error running on
MS.NET. The solution is to add the XmlInclude in the Configuration
class, as Chris pointed out.

Lluis.

On dc, 2005-05-11 at 12:49 +0100, Ricardo Gladwell wrote:
> 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
> {
> }
> _______________________________________________
> Mono-list maillist  -  Mono-list@lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-list