【问题标题】:Load XML data to array将 XML 数据加载到数组
【发布时间】:2013-12-16 18:17:44
【问题描述】:

我可能有一个简单的问题,但我就是想不通。 首先,我使用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();


    }
}

表格1:

    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 放入一个数组中。 我想将它放在一个数组中,因为我认为从数组中获取单个名称更容易,但另一方面,数组的长度取决于用户输入。

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

提前致谢!

【问题讨论】:

  • 我已经编辑了你的标题。请参阅“Should questions include “tags” in their titles?”,其中的共识是“不,他们不应该”。
  • 我认为您对加载方法有些困惑。您正在创建 GroupList 对象,但您没有返回。将 return 语句放在最后并将 void 更改为 List

标签: c# xml arrays xml-serialization


【解决方案1】:

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

【讨论】:

  • 想同样的事情,但没用:将编辑帖子我用 LoadButton 尝试过的内容
【解决方案2】:
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();

    }
}

【讨论】:

    【解决方案3】:

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

    List<Employees> GroupList;
    GroupList = (List<Employees>)xs.Deserialize(Read);
    
    ...
    String name = GroupList.Where( x => x.Name == "WhatName").Select(x => x.Name)
    

    Linq

    【讨论】:

      【解决方案4】:

      我不知道直接将数据从 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++;
              }
      

      【讨论】:

        猜你喜欢
        • 2012-09-09
        • 1970-01-01
        • 1970-01-01
        • 2012-11-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多