Convert XML file to csv file format in c#
Asked Answered
Z

4

12

I am using accord.net mouse gesture recognition 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

<?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>
Zelikow answered 25/2, 2016 at 13:27 Comment(8)
Why XML serialization you'r using ?Biscuit
i found the source code on accord.net framework website which is using XML Serialization to save file.Zelikow
I tried to save as csv but could not.Zelikow
Parse XML, write to CSV file. Which part is unclear to you?Novara
i want to write to CSV.Zelikow
Did you search? https://mcmap.net/q/108615/-writing-data-into-csv-file-in-cNovara
i only need x and y co-ordinates in my csv file. for example: 261,210,261,214,261,231,261,234Zelikow
I have file in xml format, i want to conver that in to csv format. for example: 261,210,261,214,261,231,261,234Zelikow
K
11
using System.IO;
using System.Xml.Serialization;

You can do like this:

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);
}
Keverne answered 25/2, 2016 at 14:0 Comment(3)
Above code is writing into csv file as {X=261,Y=210},{X=261,Y=214},{X=261,Y=227},{X=261,Y=229}. I need my points as 261,210,261,214,261,227,261,229Zelikow
so this is slightly buggy as a general solution, as it doesn't escape commas or quotes.Delinquent
@Delinquent this answer is for specific problem, not for general.Keverne
A
4

In an alternate way you can use leverage the power of XSLT to convert it,

Steps

  • Create an Xml stylesheet to convert xml to csv
  • Use XslCompiledTransform() to convert get the csv string
  • save the csv string to a file

You may came up with an Xslt like this, call it data.xsl

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" version="1.0" encoding="UTF-8"/>
  <xsl:template match="/">
    <xsl:for-each select="//Point">
      <xsl:value-of select="X"/>,<xsl:value-of select="Y"/>
      <xsl:text>&#xD;&#xA;</xsl:text>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

and use the following method

public static string ToCSV(string xmlTextDate, string xsltFile)
{
  string result = string.Empty;
  var xpathDoc = new XPathDocument(xmlTextDate);
  var xsltTransform = new System.Xml.Xsl.XslCompiledTransform();
  xsltTransform.Load(xsltFile);

  using (MemoryStream ms = new MemoryStream())
  {
      var writer = new XmlTextWriter(ms, Encoding.UTF8);
      using (var rd = new StreamReader(ms))
      {
          var argList = new System.Xml.Xsl.XsltArgumentList();
          xsltTransform.Transform(xpathDoc, argList, writer);
          ms.Position = 0;
          result = rd.ReadToEnd();
      }
  }
  return result;
}

and call it like this

var csvString = ToCSV("yourfile.xml","data.xsl");
Agist answered 16/6, 2018 at 13:59 Comment(0)
L
3

Converting XML directly into CSV is a little complex task. Instead of this, you can first convert your XML to DataSet and then into CSV:

  1. Converting XML to Dataset:

    DataSet ds = new DataSet();
    
    ds.ReadXml(fileNamePath);
    
  2. Converting DataTable to CSV.

    Link: c# datatable to csv

Lacuna answered 1/10, 2020 at 14:32 Comment(0)
A
-12

Just create a csv file of the XML, Use System.IO and make sure the file is something like

fileName = Name + ".csv"

Have a read up and look for something like this

Path.GetTempPath(), fileName

I am a bit sketchy but that should put you on the right track

Alleras answered 25/2, 2016 at 13:51 Comment(5)
i tried but it did not work. i only need x and y co-ordinates in my csv file.Zelikow
for example: 261,210,261,214,261,231,261,234Zelikow
Im not sure the exact syntax but it should have something to do with thatAlleras
This isn't even close to answering the OPs question.Bibbs
filename will be like "Name.csv" and Path.GetTempPath() gives Returns the path of the current user's temporary folder that won't transform xml to csv. Consider editing your post else you will keep getting downvoted.Agist

© 2022 - 2024 — McMap. All rights reserved.