用xml 串行化吧,自己读取不麻烦吗

解决方案 »

  1.   

    你现在想读取xml里面所有用户[],密码[]?
      

  2.   

    普通的xml读取方法XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load("XMLFile.xml");
    XmlNode root = xmlDoc.SelectSingleNode("root");
    int numberOfAccount = int.Parse(root.SelectSingleNode("IdNum").InnerText);
    XmlNodeList nodes = root.SelectSingleNode("Account").ChildNodes;UserInfo[] users = new UserInfo[numberOfAccount];
    for (int i = 0; i < numberOfAccount; i++)
    {
        XmlNode elm = nodes[i];
        UserInfo user = new UserInfo(){ id = elm["id"].InnerText,password = elm["password"].InnerText };
        users[i] = user;
    }class UserInfo
    {
        public string id { get; set; }
        public string password { get; set; }
    }
      

  3.   

               string xml = @"<root>
                              <IdNum>2</IdNum>
                              <Account>
                                <user>
                            <id>sunzhenggo</id>
                            <password>123456</password>
                                </user>
                                <user>
                            <id>mengmeng</id>
                            <password>654321</password>
                                </user>
                              </Account>
                            </root>";
                
                XmlDocument xdoc1 = new XmlDocument();
                xdoc1.LoadXml(xml);
                XmlNodeList lists= xdoc1.SelectNodes("root/Account/user");            Dictionary<string, string> dictUsers = new Dictionary<string, string>();            foreach (XmlNode x in lists)
                {
                    if (x!=null)
                    {
                        string key = x.SelectSingleNode("id").InnerText;
                        string value = x.SelectSingleNode("password").InnerText;
                        dictUsers.Add(key, value);
                    }
                }