[Mono-list] XmlIncludeAttribute missing class

Chris Turchin chris@turchin.net
Wed, 11 May 2005 07:58:38 -0700 (PDT)


Hi,

Sorry, I did not see that the Hashtable was private.

What I noticed when I tried your sample code is that if I decorate the
Configuration class with the XInclude's, then the error does not
occur...

[Serializable]
[XmlInclude (typeof (Folder))]
[XmlInclude (typeof (Site))]
public class Configuration {

##########

$> mcs ser.cs
$> mono ser.exe
$> more test.xml
=EF=BB=BF<?xml version=3D"1.0" encoding=3D"utf-8"?>
<Configuration xmlns:xsd=3D"http://www.w3.org/2001/XMLSchema"
xmlns:xsi=3D"http://www.w3.org/2001/XMLSchema-instance">
  <Sites>
    <anyType xsi:type=3D"ArrayOfAnyType1" />
  </Sites>
</Configuration>

Perhaps that was the issue?

Regards,
--chris


On Wed, 11 May 2005, I wrote:

> Hi,
>
> I don't think you can serialize hashtables (easily) to XML:
>
> http://blogs.msdn.com/psheill/archive/2005/04/09/406823.aspx
> http://codebetter.com/blogs/geoff.appleby/archive/2004/11/13/32005.aspx
>
> I did something similar a while back in mono with ArrayLists and that wor=
ked
> fine:
>
>         [Serializable]
>         public  class SearchList
>         {
>                 [XmlElement(typeof(SearchType), ElementName =3D "SearchTy=
pe")]
>                 public ArrayList Items =3D new ArrayList();
> etc...
>
> but I remember reading back then that hastables were a pain...
>
> Regards,
>
> --chris
>
>
>
> On Wed, 11 May 2005, 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 =3D new Configuration();
> >          Site site =3D new Site();
> >          site.Id =3D "id";
> >          configuration.Sites.Add( site );
> >          XmlSerializer serializer =3D new XmlSerializer (typeof
> > (Configuration));
> >          using (FileStream fs =3D new FileStream ("test.xml",
> > FileMode.Create)) {
> >              serializer.Serialize (fs, configuration);
> >          }
> >      }
> > }
> >
> > public class Configuration {
> >
> > =09Folder sites =3D new Folder();
> >
> > =09public Folder Sites
> > =09{
> > =09=09get {
> > =09=09=09return sites;
> > =09=09}
> > =09=09set {
> > =09=09=09this.sites =3D value;
> > =09=09}
> > =09}
> >
> > }
> >
> > [XmlInclude (typeof (Folder))]
> > [XmlInclude (typeof (Site))]
> > public abstract class Artifact {
> > =09public string Id;
> > }
> >
> > public class Folder : Artifact, IEnumerable
> > {
> > =09private Hashtable artifacts =3D new Hashtable ();
> >
> > =09public IEnumerator GetEnumerator ()
> > =09{
> > =09=09return artifacts.Values.GetEnumerator ();
> > =09}
> >
> > =09public void Add(object o) {
> >          Artifact artifact =3D (Artifact) o;
> > =09=09artifacts[artifact.Id] =3D (Artifact) artifact;
> > =09}
> > }
> >
> > public class Site : Folder
> > {
> > }
> > _______________________________________________
> > Mono-list maillist  -  Mono-list@lists.ximian.com
> > http://lists.ximian.com/mailman/listinfo/mono-list
> >
> _______________________________________________
> Mono-list maillist  -  Mono-list@lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-list
>