将XML DataTable内容转换为类

时间:2016-09-10 21:34:56

标签: c# xml datagridview dataset

这个东西让我困惑,但我会尽力解释。 我有一个包含用户的XML文件,我需要将用户加载到" User"看起来像这样的类:

public class User
{
    public string userID;
    public string pass;
    public bool banned;
    public bool online;
    public IPAddress lastIp;
    public bool admin;
    public Job currentJob; //another class just like the user class. the job will be loaded just like the users - from xml.
}

,XML文件如下所示:

<?xml version="1.0" standalone="yes"?>
<User_Table>
  <User>
    <Username>Example</Username>
    <PasswordHash>This will be hashed</PasswordHash>
    <Banned>false</Banned>
    <LastLoginIP>localhost</LastLoginIP>
    <CurrentJob>Null</CurrentJob>
    <Admin>false</Admin>
  </User>
</User_Table>

如何遍历XML文件并将每个用户加载到User类中(所有这些都将存储在List = new List()中;因此可以快速检查它们) 我已经搜索了很多,我只是无法弄清楚如何做到这一点。 我有一个用户的DataSet,datagridview显示。

2 个答案:

答案 0 :(得分:0)

您可以使用一种简单的方法通过XmlSerializer反序列化XML。首先,你需要封存你的财产:

public class User
{
    public string userID { get; set; }
    public string pass { get; set; }
    public bool banned { get; set; }
    public bool online { get; set; }
    public IPAddress lastIp { get; set; }
    public bool admin { get; set; }
    public Job currentJob { get; set; }
}

现在您可以使用XmlSerializer之类的

User DeserializedUser;
XmlSerializer Deserializer = new XmlSerializer(typeof(User));
FileStream Fs = new FileStream("UserFile.xml", FileMode.Open);
DeserializedUser = (User)Deserializer.Deserialize(Fs);

查看here了解更多信息。您也可以序列化整个集合,如

List<User> DeserializedUsers;
using (StreamReader Sr = new StreamReader("UserFile.xml"))
{
    XmlSerializer Deserializer = new XmlSerializer(typeof(List<User>), new XmlRootAttribute("User_Table"));
    DeserializedUsers = (List<User>)Deserializer.Deserialize(Sr);
}

修改 您的属性名称不能与XML文件中的名称相同。您可以将ElementName设置为

[XmlElement(ElementName = "Username")]
public string userID { get; set; }

答案 1 :(得分:0)

尝试这样的事情

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Net;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            List<User> users =  doc.Descendants("User").Select(x => new User() {
                userID = (string)x.Element("Username"),
                pass = (string)x.Element("PasswordHash"),
                banned = (bool)x.Element("Banned"),
                lastIp = Dns.GetHostAddresses((string)(x.Element("LastLoginIP"))).FirstOrDefault(),
                currentJob = x.Elements("CurrentJob").Select(y => new Job()
                {
                    name = (string)y.Element("name")
                }).FirstOrDefault(),
                admin = (bool)x.Element("Admin")
            }).ToList();
        }
    }
    public class User
    {
        public string userID { get; set; }
        public string pass { get; set; }
        public bool banned { get; set; }
        public bool online;
        public IPAddress lastIp { get; set; }
        public bool admin { get; set; }
        public Job currentJob { get; set; } //another class just like the user class. the job will be loaded just like the users - from xml.
    }
    public class Job
    {
        public string name { get; set; }
    }

}