【问题标题】:how to convert csv file to xml file in c# by columns如何在c#中按列将csv文件转换为xml文件
【发布时间】:2015-11-02 23:41:47
【问题描述】:

现在我有 csv 文件,其中包含 Worker、Account Id、Account Code、Hierarchy 和 Date 列。如何编写 c# 代码将 csv 文件转换为 xml 文件?

select new XElement("Worker",
    new XElement("Account Id", columns[0]),
    new XElement("Account Code", columns[1]),
    new XElement("Hierarchy", columns[2]),
    new XElement("Date", columns[3]),

现在我有类似的代码,我该如何改进该代码?

【问题讨论】:

  • 这对你不起作用?

标签: c# xml csv


【解决方案1】:

也许您可以通过执行以下操作来确保列名相同:

new XElement(columns[0].Key, columns[0].value)

这样您就不必连续输入每个列名,而只需使用 foreach(..) 块来生成它。

【讨论】:

    【解决方案2】:

    试试这个

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data;
    using System.Data.OleDb;
    using System.IO;
    
    namespace ConsoleApplication55
    {
        class Program
        {
            const string csvFILENAME = @"c:\temp\test.csv";
            const string xmlFILENAME = @"c:\temp\test.xml";
            static void Main(string[] args)
            {
                CSVReader reader = new CSVReader();
                DataSet ds = reader.ReadCSVFile(csvFILENAME, true);
                ds.WriteXml(xmlFILENAME, XmlWriteMode.WriteSchema);
            }
        }
        public class CSVReader
        {
    
            public DataSet ReadCSVFile(string fullPath, bool headerRow)
            {
    
                string path = fullPath.Substring(0, fullPath.LastIndexOf("\\") + 1);
                string filename = fullPath.Substring(fullPath.LastIndexOf("\\") + 1);
                DataSet ds = new DataSet();
    
                try
                {
                    if (File.Exists(fullPath))
                    {
                        string ConStr = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}" + ";Extended Properties=\"Text;HDR={1};FMT=Delimited\\\"", path, headerRow ? "Yes" : "No");
                        string SQL = string.Format("SELECT * FROM {0}", filename);
                        OleDbDataAdapter adapter = new OleDbDataAdapter(SQL, ConStr);
                        adapter.Fill(ds, "TextFile");
                        ds.Tables[0].TableName = "Table1";
                    }
                    foreach (DataColumn col in ds.Tables["Table1"].Columns)
                    {
                        col.ColumnName = col.ColumnName.Replace(" ", "_");
                    }
                }
    
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                return ds;
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

      MSDN 中有一个名为 XmlCsvReader 的类您只需指定 csv 文档所在的文件路径。然后你指定你的根的名字是 Worker,它会在加载时处理剩下的事情。唯一需要做的就是使用 Save 方法指定要输出的位置!

         XmlDocument doc = new XmlDocument(); 
         XmlCsvReader reader = new XmlCsvReader(new Uri("//yourfilepath.input.csv"), doc.NameTable); 
         reader.FirstRowHasColumnNames = true; 
         reader.RootName = "Worker"; 
         reader.RowName  = "Worker"; 
         doc.Load(reader); 
         doc.Save("output.xml");
      

      【讨论】:

        猜你喜欢
        • 2020-03-21
        • 2011-02-15
        • 1970-01-01
        • 2016-06-08
        • 1970-01-01
        • 1970-01-01
        • 2020-11-03
        • 2011-03-05
        • 2015-10-28
        相关资源
        最近更新 更多