Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a problem trying to serialize a class into XML using the xmlserializer class. The error reads:

There was an error reflecting type 'MyApplication.UserManagement'.

The debugger highlights this particular section of code:
XmlSerializer serializer = new XmlSerializer(typeof(UserManagement));


Would I be right in saying that all members in the class I want to serialize as well have to be public? I thought this could be achieved by using properties without being forced to make all the members you want to serialize public.

To help with the understanding as well I'll quickly explain the structure of the program.

1. There is a User class containing details relating to a user such as user name, date of creation etc.

2. The users are contained within a list called usersList in the UserManagement class, which also contains other members such as lastActiveUser.

3. I am trying to serialize in XML the UserManagement class, this is done in a static class I have called DataManagement which contains the following code to serialize a UserManagement instance:

public static void SerializeUserManagementXML(object obj, string pathName, string fileName)
{
    string fullPathName = Path.Combine(pathName, fileName);
            
    XmlSerializer serializer = new
    XmlSerializer(typeof(UserManagement));

    StreamWriter writer = new StreamWriter(fullPathName);
    serializer.Serialize(writer, obj);
    writer.Close();
}


EDIT: Sorry I originally posted the wrong error message.

Any help is much appreciated!
Posted
Updated 27-May-10 0:19am
v2

1 solution

Your class must be declared as public and serializable (public class UserManagement).

For default deserialization it must also have a public, parameterless constructor.
 
Share this answer
 
Comments
Walby 27-May-10 8:58am    
Ah thanks you are a life saver, I had the class set as public and serializable but didn't have a default parameterless constructor for the class within the class I was serializing.

In this case the I'm referring to the User class that is contained within the list of users, is in the UserManagement class. A bit of a school boy error, but I didn't think I would need a parameterless class for the classes held within the list but it does make sense to now when I think about it. Is this because when an object is deserialized it needs to be kind of 're-created' in a sense?

Thanks again voloda2! :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900