【问题标题】:Csv DataGridView Conversion to XML Winforms将 Csv DataGridView 转换为 XML Winforms
【发布时间】:2018-11-30 16:57:45
【问题描述】:

所以我正在处理我的项目,我想将 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# 新手,也是我在这里的第一篇文章,所以请原谅凌乱的写作或展示位置。

【问题讨论】:

    标签: c# xml winforms linq datagridview


    【解决方案1】:

    尝试以下:

                DataTable dst = new DataTable();
                int startColumn = 5;
                for(int i = dst.Columns.Count - 1; i >= startColumn; i--)
                {
                    dst = dst.AsEnumerable().OrderBy(x => dst.Columns[i]).CopyToDataTable();
                }
    

    【讨论】:

    • 我尝试了您建议我的代码,但数据仍然相同,并且相同的输出没有任何改变。我仍然得到 x0020
    • 这是由于空格。您将“On Order”作为列标题名称。 csv 的第一行用作列名和元素名。所以 WriteXml 方法是添加 0x0020(空格的 ascii 代码)。 XML 不允许元素名称中有空格。大多数人所做的是将列名中的空格替换为“_”。
    【解决方案2】:

    很抱歉回复晚了

        // 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();
    

    【讨论】:

      猜你喜欢
      • 2013-06-20
      • 2013-10-04
      • 1970-01-01
      • 1970-01-01
      • 2017-09-09
      • 1970-01-01
      • 1970-01-01
      • 2014-07-20
      相关资源
      最近更新 更多