Datagridview行到现有xml文件

本文关键字:xml 文件 Datagridview | 更新日期: 2023-09-27 18:14:50

我试图将datagridview中的每一行保存到现有的xml文件,我知道如何将文本框保存到xml文件,但我如何才能使此工作为datagridview行?

// Save values to XML profile
                    doc = new XmlDocument();
                    doc.Load(documentpath + @"'folder'" + cmb.SelectedItem + ".xml");
                    root = doc.DocumentElement;
                    root.GetElementsByTagName("Series")[0].InnerText = txtSeriesCP.Text;
                    root.GetElementsByTagName("Chassis")[0].InnerText = txtChassisCP.Text;
                    root.GetElementsByTagName("Car")[0].InnerText = txtCarCP.Text;
                    root.GetElementsByTagName("Owner")[0].InnerText = txtOwnerCP.Text;
                    root.GetElementsByTagName("Year")[0].InnerText = txtYearCP.Text;
                    root.GetElementsByTagName("VIN")[0].InnerText = txtVinCP.Text;
                    root.GetElementsByTagName("Type")[0].InnerText = txtTypeCP.Text;
                    root.GetElementsByTagName("Mileage")[0].InnerText = txtMileageCP.Text;
                    root.GetElementsByTagName("Line")[0].InnerText = txtLineCP.Text;
                    root.GetElementsByTagName("Package")[0].InnerText = txtPackageCP.Text;
                    root.GetElementsByTagName("Color")[0].InnerText = txtColorCP.Text;
                    // Save codings to XML profile
                    foreach (DataGridViewRow row in dataGridViewCP.Rows)
                    {
                        doc.CreateElement("Test");
                    }
                    doc.Save(documentpath + @"'folder'" + cmb.SelectedItem + ".xml");

我的xml是这样的

    <?xml version="1.0" encoding="utf-8"?>
<carprofile>
  <Carinfo>
    <Series></Series>
    <Chassis>F</Chassis>
    <Car></Car>
    <Type></Type>
    <Year></Year>
    <Owner></Owner>
    <VIN></VIN>
    <Mileage></Mileage>
    <Line></Line>
    <Package></Package>
    <Color></Color>
  </Carinfo>
  <Codings>
    <Applied-codings>
    </Applied-codings>
  </Codings>
</carprofile>

我想让每一行都有自己的标签在标签里,就像这样

    <Applied-codings>
<1>columnname1="test", columnname2="test2"</1>
<2>columnname1="test3", columnname2="test3"</2>
</Applied-codings>

Datagridview行到现有xml文件

我觉得你做得太难了。创建数据集并将gridview绑定到数据集要容易得多。您可以在一条指令中读取和写入数据集到xml。

            DataSet carprofileDs = new DataSet("carprofile");
            DataTable carInfoTable = new DataTable("Carinfo");
            carprofileDs.Tables.Add(carInfoTable);
            DataTable codingsTable = new DataTable("Codings");
            carprofileDs.Tables.Add(codingsTable);
            carInfoTable.Columns.Add("Series", typeof(string));
            carInfoTable.Columns.Add("Chassis", typeof(string));
            carInfoTable.Columns.Add("Car", typeof(string));
            carInfoTable.Columns.Add("Type", typeof(string));
            carInfoTable.Columns.Add("Year", typeof(string));
            carInfoTable.Columns.Add("Owner", typeof(string));
            carInfoTable.Columns.Add("Vin", typeof(string));
            carInfoTable.Columns.Add("Milage", typeof(string));
            carInfoTable.Columns.Add("Line", typeof(string));
            carInfoTable.Columns.Add("Package", typeof(string));
            carInfoTable.Columns.Add("Color", typeof(string));
            codingsTable.Columns.Add("Applied-codings", typeof(string));
            carprofileDs.WriteXml("filename", XmlWriteMode.WriteSchema);
            carprofileDs = new DataSet();
            carprofileDs.ReadXml("filename");