Tuesday, 3 February 2015

Reading in XML from WPF c#



I'm struggling to read in a "GPX" file to a WPF (c#) project. Sample GPX is provided below. I've tried a number of different options with the same result.


Document is loading ok, but I'm unable to break it down to access the Nodes Directly.


Any help would be greatly appreciated.


Thanks.



private void Simple_Click(object sender, RoutedEventArgs e)
{
XmlDocument xml = new XmlDocument();
xml.Load(@"C:\Users\Jonathon\Desktop\GPX_Data.gpx");
XmlNodeList nodes = xml.SelectNodes("trkpt"); // have tried: double '/' to get nodes at any level (XPath syntax)
//XmlNodeList nodes = xml.SelectNodes("/gpx/trk/trkseg/trkpt");

int count = 0;

foreach (XmlNode xn in nodes)
{
count++;
}

}
}


Sample GPX File



<?xml version="1.0" encoding="UTF-8"?>
<gpx version="1.1" creator="Endomondo.com" xsi:schemaLocation="http://ift.tt/JMGbbd http://ift.tt/1mgA9fA http://ift.tt/1e0pkcp http://ift.tt/1k7qZp2 http://ift.tt/1e0pkct http://ift.tt/1e0pm3Z" xmlns="http://ift.tt/JMGbbd" xmlns:gpxtpx="http://ift.tt/1e0pkct" xmlns:gpxx="http://ift.tt/1e0pkcp" xmlns:xsi="http://ift.tt/ra1lAU">
<metadata>
<author>
<name>Jonathon Ralfe</name>
<email id="jonathon" domain="ralfe.net"/>
</author>
<link href="http://ift.tt/zZESz3">
<text>Endomondo</text>
</link>
<time>2015-01-27T18:31:26Z</time>
</metadata>
<trk>
<src>http://ift.tt/1e0pkcx;
<link href="http://ift.tt/1EFleZW">
<text>endomondo</text>
</link>
<type>SKIING_DOWNHILL</type>
<trkseg>
<trkpt lat="45.576892" lon="6.894079">
<time>2015-01-26T09:49:57Z</time>
</trkpt>
<trkpt lat="45.576892" lon="6.894079">
<ele>1595.0</ele>
<time>2015-01-26T09:49:59Z</time>
</trkpt>
<trkpt lat="45.577109" lon="6.893946">
<ele>1581.0</ele>
<time>2015-01-26T09:51:46Z</time>
</trkpt>
<trkpt lat="45.5772" lon="6.894084">
<ele>1575.0</ele>
<time>2015-01-26T09:52:02Z</time>
</trkpt>
<trkpt lat="45.577247" lon="6.894212">
<ele>1577.0</ele>
<time>2015-01-26T09:52:05Z</time>
</trkpt>
<trkpt lat="45.577317" lon="6.89452">
<ele>1589.0</ele>
<time>2015-01-26T09:52:11Z</time>
</trkpt>

No comments:

Post a Comment