简体   繁体   English

将XML文件内容添加到C#中的dataGridView中?

[英]Add XML File Contents to a dataGridView in C#?

So, here's my problem. 所以,这是我的问题。 I am creating one of my first C# applications. 我正在创建我的第一个C#应用程序之一。 It is going to be a type of inventory manager. 这将是一种库存管理器。 There's a dataGridView where the data is displayed using Rows and Columns. 有一个dataGridView,其中使用行和列显示数据。 You can Add, Delete, and Update any row within the dataGridView. 您可以在dataGridView中添加,删除和更新任何行。 You can also save the contents that are in the dataGridView as an XML file. 您还可以将dataGridView中的内容另存为XML文件。 My problem is that I don't know how to load that XML file back into the dataGridView for future editing of the grid? 我的问题是我不知道如何将XML文件重新加载到dataGridView中以供将来编辑网格?

The data in the rows of the dataGridView are added using a DataTable. 使用DataTable添加dataGridView行中的数据。

Here's an example of what I mean: 这是我的意思的示例:

dataTable.Rows.Add(txtID.Text, txtName.Text);
dataGridView1.DataSource = dataTable;

I only need to get the data for the rows from the XML because the names of the columns are added when the form loads. 我只需要从XML获取行的数据,因为在表单加载时会添加列的名称。

Here's what the XML format looks like: XML格式如下所示:

<NewDataSet>
  <Table1>
    <ID>AN ID</ID>
    <Name>A NAME</Name>
  </Table1>
  <Table1>
    <ID>ANOTHER ID</ID>
    <Name>ANOTHER NAME</Name>
  </Table1>
  <Table1>
     <ID>YET ANOTHER ID AND SO ON</ID>
     <Name>YET ANOTHER NAME AND SO ON</Name>
  </Table1>
</NewDataSet>

And I don't know if this matters, but the XML file's name is Contents.xml and it is located in the application's startup location. 而且我不知道这是否重要,但是XML文件的名称是Contents.xml,它位于应用程序的启动位置。

I'm not sure if I'm making any sense? 我不确定我是否有意义?

Thanks! 谢谢! :) :)

u can do this by Serialization like 你可以通过序列化来做到这一点

   NewDataSet obj = null;
        string DeviceResponse = "";
        DeviceResponse = "<NewDataSet>" +
                "    <Table1>" +
                "      <ID>AN ID</ID>  " +
                "      <Name>A NAME</Name> " +
                "    </Table1> " +
                "    <Table1> " +
                "      <ID>ANOTHER ID</ID> " +
                "      <Name>ANOTHER NAME</Name> " +
                "    </Table1>  " +
                "    <Table1>" +
                "       <ID>YET ANOTHER ID AND SO ON</ID> " +
                "       <Name>YET ANOTHER NAME AND SO ON</Name>" +
                "    </Table1> " +
                "  </NewDataSet> ";
        try
        {
            XmlSerializer serializer = new XmlSerializer(typeof(NewDataSet));
            using (TextReader reader = new StringReader(DeviceResponse))
            {
                obj = (NewDataSet)serializer.Deserialize(reader);
            }
        }
        catch (Exception ex)
        {

        }
        //if Successful

        string id = obj.Table1[0].ID;

and your NewDataSet Class is 并且您的NewDataSet类是

[XmlRoot(ElementName = "NewDataSet", Namespace = "")]
public class NewDataSet
{
   [XmlElement("Table1")]
    public List<Table1> Table1{get; set;}
}


public class Table1
{
    public string ID {get; set;}
    public string Name {get; set;}

}

don't forget to add Namespace 不要忘记添加命名空间

using System.IO;
using System.Xml.Serialization;

You can use this way: 您可以使用这种方式:

    private void button1_Click(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();

        string xml = @"<NewDataSet>
                      <Table1>
                        <ID>AN ID</ID>
                        <Name>A NAME</Name>
                      </Table1>
                      <Table1>
                        <ID>ANOTHER ID</ID>
                        <Name>ANOTHER NAME</Name>
                      </Table1>
                      <Table1>
                         <ID>YET ANOTHER ID AND SO ON</ID>
                         <Name>YET ANOTHER NAME AND SO ON</Name>
                        <Bonus>Bonus Column</Bonus>
                      </Table1>
                    </NewDataSet>";

        using (DataSet ds = new DataSet())
        {
            using (MemoryStream mStrm = new MemoryStream(Encoding.UTF8.GetBytes(xml)))
            {
                ds.ReadXml(mStrm);
                dataGridView1.DataSource = ds.Tables[0];
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM