将XML数据加载到数组

时间:2013-12-16 18:17:45

标签: c# xml arrays xml-serialization

我可能有一个简单的问题,但我无法弄清楚。 首先,我使用此Source将数据保存到XML文件。 头等舱:

public class Employees
{

    public string Name { get; set; }

    public Employees() { }

    public Employees(string name)
    {
        Name = name;
    }

}

public class group
{
    public string Name { get; set; }

    public List<Employees> Employees { get; set; }

    public group()
    {
        Employees = new List<Employees>();
    }

将XML数据保存在另一个类中。

 public class SaveXML
{
    static public void SaveData(group info)
    {
        XmlSerializer xs = new XmlSerializer(info.GetType());

        StreamWriter writer = new StreamWriter("dataArray.xml");
        xs.Serialize(writer, info);
        writer.Close();


    }
}

Form1中:

    private void SaveButton_Click(object sender, EventArgs e)
    {
        string[] employees = new string[EmployList.Items.Count];
        for (int x = 0; x < EmployList.Items.Count; x++)
        {
            employees[x] = EmployList.Items[x].ToString();
        }

        try
        {
            group groups = new group();

            groups.Name = GroupText.Text;
            for (int x = 0; x < employees.Length; x++)
            {
                groups.Employees.Add(new Employees(employees[x]));
            }

            SaveXML.SaveData(groups);
        }
        catch (Exception es)
        {
            MessageBox.Show(es.Message);
        }

    }

现在我想将这些数据加载到数组中, 这是我的加载数据类:

public class LoadXML
{
    static public void LoadData(group LoadInfo)
    {
        XmlSerializer xs = new XmlSerializer(LoadInfo.GetType());
        TextReader Read = new StreamReader("dataxml.xml");
        List<Employees> GroupList;
        GroupList = (List<Employees>)xs.Deserialize(Read);
    }
}

这是我尝试过的,但它给了我错误:

    private void LoadButton_Click(object sender, EventArgs e)
    {
        group groups = new group();
        LoadXML Loading = new LoadXML();
        Loading.LoadData(groups);
        string[] array = new string[10];
        array = groups.Employees.ToArray; //error line

    }

所以现在我的问题是如何将GroupList放入main函数中的Array中。 我希望将它放在一个数组中因为我认为从数组中获取单个名称更容易,但另一方面,数组的长度取决于用户输入。

这是一个测试程序,而不是我正在使用的实际程序,这只是学习它然后在我的原始项目中实现它。

提前致谢!

4 个答案:

答案 0 :(得分:0)

如果GroupList是List,或者它包含列表中包含员工的属性。如果你有一个名为groups的变量,你可以调用GroupList.ToArray()或groups.Employees.ToArray()。

答案 1 :(得分:0)

public class LoadXML
{
    static public void LoadData(group LoadInfo)
    {
        XmlSerializer xs = new XmlSerializer(LoadInfo.GetType());
        TextReader Read = new StreamReader("dataxml.xml");
        List<Employees> GroupList;
        GroupList = (List<Employees>)xs.Deserialize(Read);

       // Now you have an array of Employees - but why do you need it. you have already a list class. it's verymuch flexible to implement a List class

        Employee [] employeeArray = GroupList.ToArray();

    }
}

答案 2 :(得分:0)

您可以使用LINQ来选择名称或任何您想要的名称。这很简单

List<Employees> GroupList;
GroupList = (List<Employees>)xs.Deserialize(Read);

...
String name = GroupList.Where( x => x.Name == "WhatName").Select(x => x.Name)

<强> Linq

答案 3 :(得分:0)

我不知道如何直接将数据从XML填充到数组,但是您可以使用DATASET对象从xml文件中读取数据

        DataSet ds = new DataSet();
        ds.ReadXml("Path for xml file");
        int[] arr=new arr[ds.rows.count-1];
        int i=0;
        foreach (DataRow dr in ds.Tables[0].Rows)
        {
            arr[i]=dr["Field Name in xml file"].ToString();
            i++;
        }