写了个小函数,从XML文件转换到CSV文件的

第一次用C#里面的XmlDataDocument类,发现真好用:)

  public void Transfer(string inXmlFilename, string outCSVFilename)
  {
   // open or create
   FileStream fileCsv = new FileStream(outCSVFilename,FileMode.OpenOrCreate,FileAccess.Write);

   StreamWriter writerCsv = new StreamWriter(fileCsv);
   StreamReader readerXml=new StreamReader(inXmlFilename);
   
   XmlDataDocument datadoc = new XmlDataDocument();    
   datadoc.DataSet.ReadXml(readerXml);

   // read each test case, write to CSV file
   for (int i= 0; i < datadoc.ChildNodes[0].ChildNodes.Count; i++)
   {
    string strCsvLine = "";
    for (int j=0; j < datadoc.ChildNodes[0].ChildNodes[i].ChildNodes.Count; j++)
    {
     string strText = datadoc.ChildNodes[0].ChildNodes[i].ChildNodes[j].InnerText;   
     strCsvLine += strText.Replace(",", ".") + ",";
    }

    writerCsv.WriteLine(strCsvLine);
   }
   
   readerXml.Close();
   writerCsv.Close();
  }

展开阅读全文

没有更多推荐了,返回首页