Link to home
Start Free TrialLog in
Avatar of Phreak3eb
Phreak3eb

asked on

XmlSerialization of Custom Type

I've been trying to get this to work all morning and can't seem to get it to work.  Microsoft say's it's not possible to use the XmlSerializer to serialize an array of List<T> and ArrayList according to http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx.

However, according to this article and a lot of user comments below it, it says you can.
http://www.hanselman.com/blog/XmlSerializingAGenericListltgt.aspx

This is the error I'm receiving:
There was an error reflecting type 'EFC.SupportServices.Management.Classes.Department'.

This is the InnerException:
"Cannot serialize member 'EFC.SupportServices.Management.Classes.Department.Groups' of type 'System.Collections.Generic.List`1[[EFC.SupportServices.Management.Classes.Group, EFC.SupportServices.Management, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]', see inner exception for more details."

I don't necessarily have to use List<T>, but need some way of holding a collection of the other types.  So anyway to get this to work will probably suffice.

Here are the classes I'm trying to serialize:


[DataContract(), XmlRoot()]
    [KnownType(typeof(Group))]
    [KnownType(typeof(Role))]
    public class Department
    {
        private List<Group> _Groups;
        private List<Role> _Role;
 
        public string Name { get; set; }
 
        [DataMember()]
        public List<Group> Groups
        {
            get
            {
                if (_Groups == null)
                {
                    _Groups = new List<Group>();
                }
                return _Groups;
            }
            set
            {
                _Groups = value;
            }
        }
 
        [DataMember]
        public List<Role> Role
        {
            get
            {
                if (_Role == null)
                {
                    _Role = new List<Role>();
                }
                return _Role;
            }
            set
            {
                _Role = value;
            }
        }
    }
 
    [DataContract()]
    public class Role
    {
        public Role(string Name)
        {
            Title = Name;
        }
 
        [DataMember()]
        public string Title { get; set; }
 
    }
 
    [DataContract()]
    public class Group
    {
        public Group (string GroupName)
        {
            Name = GroupName;
        }
 
        public Group (string GroupName, string GroupDisplayName)
        {
            Name = GroupName;
            DisplayName = GroupDisplayName;
        }
 
        [DataMember()]
        public string Name { get; set; }
 
        [DataMember()]
        public string DisplayName { get; set; }
    }

Open in new window

Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

You can serialize the class by yourself deriving your class from the IXmlSerializable interface:



public class Department : IXmlSerializable
{ 
    private List<Group> _Groups;
    private List<Role> _Role;
 
    public string Name { get; set; }
 
    //
    // your class implementation here
    //
 
    // Xml Serialization Infrastructure
    public void WriteXml (XmlWriter writer)
    {
        writer.WriteElementString("Name", Name);
        writer.WriteStartElement("Groups");
        foreach (Group g in Groups)
        {
            writer.WriteStartElement("Group");
                writer.WriteElementString("Name", g.Name);
                writer.WriteElementString("DisplayName", g.DisplayName);
            writer.WriteEndElement();
        } 
        writer.WriteEndElement();
        writer.WriteStartElement("Roles");
        foreach (Role r in Roles)
        {
            writer.WriteStartElement("Role");
                writer.WriteElementString("Title", r.Title);
            writer.WriteEndElement();
        } 
        writer.WriteEndElement();
    }
    public void ReadXml (XmlReader reader)
    {
        // to be implemented
    }
    public XmlSchema GetSchema()
    {
        return(null);
    }
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Phreak3eb
Phreak3eb

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial