We help IT Professionals succeed at work.
Get Started

XmlSerialization of Custom Type

Phreak3eb
Phreak3eb asked
on
807 Views
Last Modified: 2013-12-17
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

Comment
Watch Question
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE