Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将DataGridView的数据添加到数据集_C#_.net_Xml_Dataset - Fatal编程技术网

C# 将DataGridView的数据添加到数据集

C# 将DataGridView的数据添加到数据集,c#,.net,xml,dataset,C#,.net,Xml,Dataset,如何将DataGridView的数据添加到数据集(包括列名) 我在下面的代码中开始并结巴了: DataTable table = new DataTable(); DataRow newRow = new DataRow(); table.Columns.Add("productname"); //first column table.Columns.Add("brandname");

如何将DataGridView的数据添加到数据集(包括列名)

我在下面的代码中开始并结巴了:

             DataTable table = new DataTable();
             DataRow newRow = new DataRow();
             table.Columns.Add("productname");  //first column
             table.Columns.Add("brandname");    //second column
             table.Columns.Add("quantity");     //third column
             table.Columns.Add("price");        //fourth column
然后我需要像这样将这个数据集写入一个XML文件

<stock>
    <items>   //How to add these two extra tags? ('stock' and 'items')
       ----Column Names----
    <items>
<stock>

//如何添加这两个额外的标签?(“库存”和“项目”)
----列名----
请帮忙
提前谢谢

DataSet ds = new DataSet("stock");
DataTable table = new DataTable("items");
table.Columns.Add("productname");  //first column
table.Columns.Add("brandname");    //second column
table.Columns.Add("quantity");     //third column
table.Columns.Add("price");        //fourth column
如上所述命名数据集和数据表。它将根据需要编写xml

 DataRow newRow = table.NewRow();
newRow["productname"] = "some value";
newRow["brandname"] = "some value";
newRow["quantity"] = "some value";
newRow["price"] = "some value";
table.Rows.Add(newRow);
ds.Tables.Add(table);
编辑:

string xml = ds.GetXml();

谢谢你的回复。然后,如何将它添加到带有两个额外标记的xml文件中,我在前面提到过。我知道如何写,但我不知道如何添加额外的标记。不,实际上我需要向创建的表中添加两个以上的父元素,即“stock”和“items”。正如我在问题中提到的