Output XML data to CSV text file using C# XDocument -
i using visual studio 2015 , working visual c# / xaml wpf project.
what have .xml document contains values read, manipulate, , output results comma delimited .csv file specific headers. believe have got of code correct, there seems issue levels of nested elements. code creates .csv, blank.
i've experimented .descendants() , .elements() part of code, cannot written file. think can achieve goal using xdocument class , wish not use other resources if possible.
any appreciated! first time posting here if additional information needed, glad add more.
c# code:
private void testmethod() { stringbuilder sb = new stringbuilder(); string delimiter = ","; xdocument.load(@"c:\users\paulie\desktop\flight1.xml") .descendants("trk") .elements() .tolist() .foreach(element => sb.append( element.attribute("lon").value + delimiter + element.attribute("lat").value + delimiter + element.element("ele").value + delimiter + element.element("time").value + delimiter + element.element("course").value + delimiter + element.element("pitch").value + delimiter + element.element("roll").value + "\r\r")); streamwriter sw = new streamwriter(@"c:\users\paulie\desktop\flight1.csv"); sw.writeline(sb.tostring()); sw.close(); console.writeline(sb.tostring()); }
xml source sample (the unfriendly version, written in xml text):
<gpx creator="mission planner 1.3.29 build 1.1.5646.37690" xmlns="http://www.topografix.com/gpx/1/1"><trk><trkseg><trkpt lat="30.3020034" lon="-96.4781874"><ele>89.96</ele><time>2016-08-30t08:46:16-05:00</time><course>56.32</course><roll>-3.11</roll><pitch>-5.99</pitch><mode /></trkpt><trkpt lat="30.3020015" lon="-96.4781862"><ele>89.45</ele><time>2016-08-30t08:46:19-05:00</time><course>56.3</course><roll>-3.06</roll><pitch>-6.06</pitch><mode /></trkpt><trkpt lat="30.3020013" lon="-96.4781861"><ele>89.42</ele><time>2016-08-30t08:46:19-05:00</time><course>56.3</course><roll>-3.06</roll><pitch>-6.11</pitch><mode /></trkpt><trkpt lat="30.3020012" lon="-96.478186"><ele>89.38</ele><time>2016-08-30t08:46:19-05:00</time><course>56.29</course><roll>-3.05</roll><pitch>-6.14</pitch><mode /></trkpt><trkpt lat="30.302001" lon="-96.4781859"><ele>89.35</ele><time>2016-08-30t08:46:20-05:00</time><course>56.29</course><roll>-3.06</roll><pitch>-6.13</pitch><mode /></trkpt><trkpt lat="30.3020009" lon="-96.4781858"><ele>89.32</ele><time>2016-08-30t08:46:20-05:00</time><course>56.28</course><roll>-3.07</roll><pitch>-6.17</pitch><mode /></trkpt><trkpt lat="30.3020007" lon="-96.4781856"><ele>89.29</ele><time>2016-08-30t08:46:20-05:00</time><course>56.27</course><roll>-3.08</roll><pitch>-6.17</pitch><mode /></trkpt><trkpt lat="30.3020005" lon="-96.4781855"><ele>89.25</ele><time>2016-08-30t08:46:20-05:00</time><course>56.26</course><roll>-3.1</roll><pitch>-6.2</pitch><mode /></trkpt><trkpt lat="30.3020004" lon="-96.4781854"><ele>89.22</ele><time>2016-08-30t08:46:20-05:00</time><course>56.26</course><roll>-3.1</roll><pitch>-6.21</pitch><mode /></trkpt><trkpt lat="30.3020003" lon="-96.4781853"><ele>89.2</ele><time>2016-08-30t08:46:21-05:00</time><course>56.25</course><roll>-3.1</roll><pitch>-6.24</pitch><mode /></trkpt><trkpt lat="30.3020002" lon="-96.4781852"><ele>89.17</ele><time>2016-08-30t08:46:21-05:00</time><course>56.24</course><roll>-3.12</roll><pitch>-6.26</pitch><mode /></trkpt><trkpt lat="30.302" lon="-96.4781851"><ele>89.16</ele><time>2016-08-30t08:46:21-05:00</time><course>56.23</course><roll>-3.12</roll><pitch>-6.29</pitch><mode /></trkpt><trkpt lat="30.3019999" lon="-96.478185"><ele>89.13</ele><time>2016-08-30t08:46:21-05:00</time><course>56.23</course><roll>-3.12</roll><pitch>-6.32</pitch><mode /></trkpt><trkpt lat="30.3019998" lon="-96.4781848"><ele>89.09</ele><time>2016-08-30t08:46:22-05:00</time><course>56.2</course><roll>-3.09</roll><pitch>-6.36</pitch><mode /></trkpt><trkpt lat="30.3019997" lon="-96.4781847"><ele>89.08</ele><time>2016-08-30t08:46:22-05:00</time><course>56.2</course><roll>-3.09</roll><pitch>-6.38</pitch><mode /></trkpt><trkpt lat="30.3019996" lon="-96.4781846"><ele>89.06</ele><time>2016-08-30t08:46:22-05:00</time><course>56.21</course><roll>-3.1</roll><pitch>-6.28</pitch><mode /></trkpt><trkpt lat="30.3019995" lon="-96.4781845"><ele>89.04</ele><time>2016-08-30t08:46:22-05:00</time><course>56.2</course><roll>-3.1</roll><pitch>-6.22</pitch><mode /></trkpt></trkseg></trk></gpx>
expected output sample:
timestamp,sensorlatitude,sensorlongitude,sensoraltitude,platformheading,platformpitch,platformroll 1472525176000000,30.3020034,-96.4781874,89.96,56.32,-5.99,-3.11 1472525179000000,30.3020015,-96.4781862,89.45,56.3,-6.06,-3.06 1472525179000000,30.3020013,-96.4781861,89.42,56.3,-6.11,-3.06 1472525179000000,30.3020012,-96.478186,89.38,56.29,-6.14,-3.05 1472525180000000,30.302001,-96.4781859,89.35,56.29,-6.13,-3.06 1472525180000000,30.3020009,-96.4781858,89.32,56.28,-6.17,-3.07
(note timestamp column contains integer values instead of date-time format. 1 field need change using function converts date-time format posix/unix time signature)
elements()
gets child elements. when this:
.descendants("trk").elements()
you selecting trkseg
elements (the child elements of trk
elements). data inside of trkpt
elements, want this:
xdocument.load(@"c:\users\paulie\desktop\flight1.xml") .descendants("trkpt") .foreach(...);
additionally, because xml in namespace, need specify when looking nodes:
.descendants("{http://www.topografix.com/gpx/1/1}trkpt")
Comments
Post a Comment