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

C#:将xml文件转换为csv文件,c#,xml,linq,csv,visual-studio-2010,C#,Xml,Linq,Csv,Visual Studio 2010,我有多个.xml文件,它们都有相同的节点名,但值不同。例如: File1.xml包含以下内容: <?xml version="1.0"?><Data><Waf>No</Waf><Name>TEMP\1</Name><Number>0</Number><Iteration>1</Iteration><Lot> </Lot><DateAndTime&g

我有多个.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>
我的算法的输入是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);
    }
}
for(idx=0;idx
但是,

  • 节点数xml_num_节点输出为2
  • 我认为我没有必要从头开始写这段代码,必须有更简单的方法。如果是,我错过了什么?我正在使用Linq,看到了一个fer资源,但我无法得到我想要的
定义一个类来接受反序列化的XML数据,然后将每个XML文件反序列化到该类中,然后迭代类成员并将每个成员的数据写入CSV字符串,最后将CSV字符串写入输出CSV文件


参考:

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


参考:

使用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();
        }
    }

}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
使用System.IO;
命名空间控制台应用程序1
{
班级计划
{
常量字符串文件夹=@“c:\temp\”;
常量字符串CSV_文件名=@“c:\temp\test.CSV”;
静态void Main(字符串[]参数)
{
字符串[]xmlFiles=Directory.GetFiles(文件夹“*.xml”);
StreamWriter writer=新的StreamWriter(CSV_文件名);
布尔第一行=真;
for(intidx=0;idxx.Name.LocalName.ToArray();
WriteLine(string.Join(“,”,headers));
firstLine=false;
}
string[]row=data.Elements()。选择(x=>(string)x.ToArray();
writer.WriteLine(string.Join(“,”,row));
}
}
writer.Flush();
writer.Close();
}
}
}

使用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();
        }
    }

}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
使用System.IO;
命名空间控制台应用程序1
{
班级计划
{
常量字符串文件夹=@“c:\temp\”;
常量字符串CSV_文件名=@“c:\temp\test.CSV”;
静态void Main(字符串[]参数)
{
字符串[]xmlFiles=Directory.GetFiles(文件夹“*.xml”);
StreamWriter writer=新的StreamWriter(CSV_文件名);
布尔第一行=真;
for(intidx=0;idxx.Name.LocalName.ToArray();
WriteLine(string.Join(“,”,headers));
firstLine=false;
}
string[]row=data.Elements()。选择(x=>(string)x.ToArray();
writer.WriteLine(string.Join(“,”,row));
}
}
writer.Flush();
writer.Close();
}
}
}

dea将xml序列化为一个具体类,使用专用CSV库(可能只接受一个具体类或接口)将其序列化;dea将xml序列化为一个具体类,使用专用CSV库(可能只接受一个具体类或接口)将其序列化
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();
        }
    }

}