Csv DataGridView转换为XML Winforms

时间:2018-11-30 16:57:46

标签: c# xml winforms linq datagridview

所以我正在做我的项目,我想将CSV文件中的datagridview写入XML文件,我已经实现了这一点,但是我想知道是否有任何方法可以对订单视图进行排序或更改结果XML的我想要的是对特定列中的字母顺序进行排序。这是我保存XML文件的代码。

    if (saveFileDialogXml.ShowDialog() == DialogResult.OK)
        {
            //Xml Alphabetical order code goes here

            DataTable dst = new DataTable();
            dst = (DataTable)Datagridview1.DataSource;
            dst.TableName = "Data";
            dst.WriteXml(saveFileDialogXml.FileName);

        }

        }

但是它的输出是

<?xml version="1.0" standalone="yes"?>

  

<Item_x0020_Code>Item Code</Item_x0020_Code>
<Item_x0020_Description>Item Description</Item_x0020_Description>
<Current_x0020_Count>Current Count</Current_x0020_Count>
<On_x0020_Order>On Order</On_x0020_Order>

如您所见,它甚至将十六进制数放进去,并且它只是将所有内容扔在那里,所以我想知道是否可以重新格式化它,使其显示方式像删除 x0020 一样。因此,我尝试使用LINQ来查看文件是否存在问题,但我不断收到另一个错误,提示

  

System.Xml.XmlException:'''字符,十六进制值0x20,不能包含在名称中。'

这是LINQ代码:

var xmlFile = new XElement("root",
                from line in File.ReadAllLines(@"C:\\StockFile\stocklist.csv")
                .Where(n => !string.IsNullOrWhiteSpace(n))
                where !line.StartsWith(",") && line.Length > 0
                let parts = line.Split(',')
                select new XElement("Item Code",
                                    new XElement("Test1", parts[0]),
                                    new XElement("Test2", parts[1])

                                    )

             );

此外,我是C#的新手,也是我的第一篇文章,所以请原谅凌乱的文字或位置。

2 个答案:

答案 0 :(得分:0)

尝试以下操作:

<li>

答案 1 :(得分:0)

很抱歉收到我的答复,所以我有点想通了,所以如果你们中有人遇到同样的事情,我还是忘了关闭或标记答案

    // Save file dialogue XML file.
        if (saveFileDialogXml.ShowDialog() == DialogResult.OK)
        {

           //try block to catch exception and handle it.
            try
            {
               //Changing Data Table name to stock.
                string Stock = ((DataTable)Datagridview1.DataSource).TableName;
            }

            //Catching the exception and handling it.
            catch (Exception)
            {
                string es = "Please Open The File Before Saving it";
                string title = "Error";
                MessageBox.Show(es, title);


            }

// instatiate new DataTable.
            DataTable dt = new DataTable
            {
                TableName = "Stock"
            };

            for (int i = 0; i < Datagridview1.Columns.Count; i++)
            {
                //if (dataGridView1.Columns[i].Visible) // Add's only Visible columns.
                //{
                string headerText = Datagridview1.Columns[i].HeaderText;
                headerText = Regex.Replace(headerText, "[-/, ]", "_");

                DataColumn column = new DataColumn(headerText);
                dt.Columns.Add(column);
                //}
            }

            foreach (DataGridViewRow DataGVRow in Datagridview1.Rows)
            {
                DataRow dataRow = dt.NewRow();
                // Add's only the columns that I need
                dataRow[0] = DataGVRow.Cells["Item Code"].Value;
                dataRow[1] = DataGVRow.Cells["Item Description"].Value;
                dataRow[2] = DataGVRow.Cells["Current Count"].Value;
                dataRow[3] = DataGVRow.Cells["On Order"].Value;

                dt.Rows.Add(dataRow); //dt.Columns.Add();
            }
            DataSet ds = new DataSet();
            ds.Tables.Add(dt);

           //Finally the save part:
            XmlTextWriter xmlSave = new XmlTextWriter(saveFileDialogXml.FileName, Encoding.UTF8)
            {
                Formatting = Formatting.Indented
            };
            ds.DataSetName = "Data";
            ds.WriteXml(xmlSave);
            xmlSave.Close();