【问题标题】:C#: convert xml file to csv fileC#:将 xml 文件转换为 csv 文件
【发布时间】:2020-03-21 07:25:48
【问题描述】:

我有多个 .xml 文件,它们都具有相同的节点名称但不同的值。示例:

File1.xml 有以下内容:

<?xml version="1.0"?><Data><Waf>No</Waf><Name>TEMP\1</Name><Number>0</Number><Iteration>1</Iteration><Lot> </Lot><DateAndTime>11:36:24:35 10/8/2019</DateAndTime><Id>5555</Id><SW>6.40.22.10900</SW><Image>Reference Point 750</Image><Angle >0</Angle ><Algo></Algo></Data>

同样,File2.xml 有:

<?xml version="1.0"?><Data><Waf>Yes</Waf><Name>TEMP\2</Name><Number>10</Number><Iteration>6</Iteration><Lot>99</Lot><DateAndTime>11:36:49:35 10/8/2019</DateAndTime><Id></Id><SW>6.40.22.10900</SW><Image>Reference Point 90</Image><Angle >180</Angle ><Algo></Algo></Data>

我使用 C# (Visual Studio 2010);我的目标是获取具有第一行的 .csv / .txt 文件:

Waf, Name, Number, Iteration, Lot, DateAndTime, Id, SW, Image, Angle,   Algo
No, TEMP\1, 0, 1, - , 11:36:24:35 10/8/2019, 5555, 6.40.22.10900, Reference Point 750, 0, -    
Yes, TEMP\2 , 10, 6, 99, 11:36:49:35 10/8/2019, -, 6.40.22.10900, Reference Point 90, 180, -    

我的算法的输入是 xml 文件的名称。这些是我到目前为止所做的步骤:

for (idx = 0; idx < num_files; idx++)
{
    file_name = file_name + ".xml"; // this contains the name of xml file
    if (idx == 0)   // if I'm reading the first xml file, make a note of all the node names since they will be the column headers. 
    {
      fs = new FileStream(location_xml_file, FileMode.Open, FileAccess.Read);
      xmldoc.Load(fs);
      xml_num_nodes = xmldoc.n  ; //.Count;
      Console.Write("\n xml_num_nodes = {0}", xml_num_nodes);
    }
}

然而,

  • 节点数xml_num_nodes输出为2。
  • 我认为我没有必要从头开始编写这段代码,而且必须有更简单的方法。如果是这样,我错过了什么?我正在使用 Linq 并看到了 fer 资源,但我无法获得我想要的东西。

【问题讨论】:

  • 将 xml 反序列化为一个具体的类,使用专用的 CSV 库对其进行序列化(可能只需要一个具体的类或接口)

标签: c# xml linq csv visual-studio-2010


【解决方案1】:

使用 xml linq 非常简单的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        const string FOLDER = @"c:\temp\";
        const string CSV_FILENAME = @"c:\temp\test.csv";

         static void Main(string[] args)
         {
            string[] xmlFiles = Directory.GetFiles(FOLDER, "*.xml");
            StreamWriter writer = new StreamWriter(CSV_FILENAME);
            Boolean firstLine = true;
            for (int idx = 0; idx < xmlFiles.Length; idx++)
            {
                string file_name = xmlFiles[idx]; 
                XDocument doc  = XDocument.Load(file_name);

                foreach(XElement data in doc.Descendants("Data"))
                {
                    if (firstLine)
                    {
                        string[] headers = data.Elements().Select(x => x.Name.LocalName).ToArray();
                        writer.WriteLine(string.Join(",", headers));
                        firstLine = false;
                    }
                    string[] row = data.Elements().Select(x => (string)x).ToArray();
                    writer.WriteLine(string.Join(",", row));
                }
            }
            writer.Flush();
            writer.Close();
        }
    }

}

【讨论】:

    【解决方案2】:

    定义一个类来接受反序列化的 XML 数据,然后将每个 XML 文件反序列化到类中,然后迭代类成员并将每个成员的数据写入 CSV 字符串,最后将 CSV 字符串写入您的输出 CSV文件。

    参考:http://www.janholinka.net/Blog/Article/11

    【讨论】:

      猜你喜欢
      • 2011-03-05
      • 2015-10-28
      • 2014-11-22
      • 2017-04-18
      • 1970-01-01
      • 2016-06-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多