C#尝试将ArrayList序列化为XML文件

C#尝试将ArrayList序列化为XML文件,c#,xml,serialization,C#,Xml,Serialization,因此,我尝试在ArrayList中存储名称/密码组合,并尝试将此ArrayList保存到一个文件中,并在每次程序启动时加载它。但是,程序在序列化程序处停止。序列化(sw,输入)符合以下要求的行: System.Xml.dll中发生类型为“System.InvalidOperationException”的未处理异常 我做错了什么?我们开始吧;我认为这解决了几乎所有的问题 struct ClientInfo { public string strName; //Name

因此,我尝试在ArrayList中存储名称/密码组合,并尝试将此ArrayList保存到一个文件中,并在每次程序启动时加载它。但是,程序在
序列化程序处停止。序列化(sw,输入)符合以下要求的行:

System.Xml.dll中发生类型为“System.InvalidOperationException”的未处理异常


我做错了什么?

我们开始吧;我认为这解决了几乎所有的问题

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();
    }
public class ClientInfo//你是说“class”对吗?因为这显然不是一个“价值”
{
公共字符串名称{get;set;}//使用属性;不要使用名称前缀
公共字符串密码{get;set;}//请告诉我您没有存储密码
}
List clientList=新列表();//键入列表
公共静态void序列化(列表输入)//类型化列表
{
如果(input==null)抛出新的ArgumentNullException(“input”);
XmlSerializer serializer=新的XmlSerializer(typeof(List));
使用(TextWriter sw=new StreamWriter(“users.txt”)//因为:IDisposable
{
序列化器。序列化(软件,输入);
sw.Close();
}
}

顺便问一下,你有什么理由不能用
列表
代替
数组列表
;如果你看一下异常的
.InnerException
XmlSerializer
实际上非常擅长给出它不能做某事的详细原因这是一个学校项目,老师给出了他使用struct的程序的基本版本,这就是我坚持使用它的原因。这也是为什么我要这样存储密码的原因:P@user3390443然后请代表我告诉你的老师,他们给出了非常不正确的建议,如果他们想讨论为什么以及如何正确地做,我非常愿意通过电子邮件、Skype和,或者他们选择的任何东西。@user或者换一种说法:我给你的老师10分中的1分,我用的是红笔
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();
    }
}