troubleshooting Question

XmlSerialization of Custom Type

Avatar of Phreak3eb
Phreak3eb asked on
.NET ProgrammingC#
2 Comments1 Solution809 ViewsLast Modified:
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

ASKER CERTIFIED SOLUTION
Phreak3eb

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros