1
struct ClientInfo
    {
        public string strName;  //Name by which the user logged into the chat room
        public string strPW;
    }

    ArrayList clientList = new ArrayList();

    public static void Serialize(ArrayList input)
    {
        XmlSerializer serializer = new XmlSerializer(input.GetType());
        TextWriter sw = new StreamWriter("users.txt");
        serializer.Serialize(sw, input);
        sw.Close();
    }

So I am trying to store Name/Password combinations in an ArrayList and I am trying to save this ArrayList into a file, and load it every time the program is started. However, the program stops at the serializer.Serialize(sw, input); line with the following:

An unhandled exception of type 'System.InvalidOperationException' occurred in System.Xml.dll

What am I doing wrong ?

2
  • Is there any reason you can't use List<ClientInfo> instead of ArrayList ? Commented Nov 26, 2014 at 14:52
  • By the way; if you look at the .InnerException of the exception, XmlSerializer is actually very good at giving detailed reasons for why it can't do something Commented Nov 26, 2014 at 14:57

1 Answer 1

1

Here we go; I think this fixes just about all of the problems...

public class ClientInfo // you meant "class" right? since that clearly isn't a "value"
{
    public string Name {get;set;} // use a property; don't use a name prefix
    public string Password {get;set;} // please tell me you aren't storing passwords
}

List<ClientInfo> clientList = new List<ClientInfo>(); // typed list

public static void Serialize(List<ClientInfo> input) // typed list
{
    if(input == null) throw new ArgumentNullException("input");
    XmlSerializer serializer = new XmlSerializer(typeof(List<ClientInfo>));
    using(TextWriter sw = new StreamWriter("users.txt")) // because: IDisposable
    {
        serializer.Serialize(sw, input);
        sw.Close();
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

This is a school project, the teacher gave out a basic version of the program in which he used struct, thats why I sticked with it. Thats also the reason why I am storing passwords like this :P
@user3390443 then please tell your teacher on my behalf that they are giving out very incorrect advice, and that if they would like to discuss why and how to do it properly, I'll be more than willing to discuss it with them briefly or at length, via email, Skype, or whatever they choose.
@user or to put it another way: I give your teacher 1 out of 10, and I used red pen

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.