本文介绍了将XML文件转换为c#中的csv文件格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

<?xml version="1.0"?>
<ArrayOfSequence xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Sequence>

    <SourcePath>
      <Point>
        <X>261</X>
        <Y>210</Y>
      </Point>
      <Point>
        <X>261</X>
        <Y>214</Y>
      </Point>
      <Point>
        <X>261</X>
        <Y>227</Y>
      </Point>
      <Point>
        <X>261</X>
        <Y>229</Y>
      </Point>
      <Point>
        <X>261</X>
        <Y>231</Y>
      </Point>
      <Point>
        <X>261</X>
        <Y>234</Y>
      </Point>
      <Point>
        <X>261</X>
        <Y>237</Y>
      </Point>
</Sequence>
</ArrayOfSequence>

我正在使用Accord.net鼠标手势识别示例应用程序,该应用程序以上述xml格式保存文件.我需要将上述xml转换为CSV格式的帮助,因此我可以使用Accord.net动态时间扭曲来进行机器学习.我不知道如何转换为csv文件.例如: 261,210,261,214,261,229,261,231

I am using accord.net mouse gesture recogination sample application, which saves the file in above xml format.I need help to convert above xml in to CSV format so i can do machine learning using accord.net Dynamic time warping.I can not figure out how to convert in to csv file.for example: 261,210,261,214,261,229,261,231

推荐答案

using System.IO;
using System.Xml.Serialization;

您可以这样做:

public class Sequence
{
    public Point[] SourcePath { get; set; }
}

using (FileStream fs = new FileStream(@"D:\youXMLFile.xml", FileMode.Open))
{
    XmlSerializer serializer = new XmlSerializer(typeof(Sequence[]));
    var data=(Sequence[]) serializer.Deserialize(fs);
    List<string> list = new List<string>();
    foreach(var item in data)
    {
        List<string> ss = new List<string>();
        foreach (var point in item.SourcePath) ss.Add(point.X + "," + point.Y);
        list.Add(string.Join(",", ss));
    }
    File.WriteAllLines("D:\\csvFile.csv", list);
}

这篇关于将XML文件转换为c#中的csv文件格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 20:57