C#:将 xml 文件转换为 csv 文件

C#: convert xml file to csv file

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

File1.xml有以下内容:

<?xml version="1.0"?><Data><Waf>No</Waf><Name>TEMP</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</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, 0, 1, - , 11:36:24:35 10/8/2019, 5555, 6.40.22.10900, Reference Point 750, 0, -    
Yes, TEMP , 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);
    }
}

然而,

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

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

代码如果使用 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();
        }
    }

}