简体   繁体   中英

VB.Net GPX to Datatable

I am trying to convert a GPX file to a datatable but seem to be really struggling with the basics of this. A GPX file is just an XML for GPS data. I have managed to put the URI of the file into a variable with a dialog box.

Here is the GPX file I am trying to load (albeit with a reduced number of points):

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<gpx xmlns="http://www.topografix.com/GPX/1/1"
xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3"
xmlns:gpxtpx="http://www.garmin.com/xmlschemas/TrackPointExtension/v1"
xmlns:badelf="http://bad-elf.com/xmlschemas"
version="1.1"
creator="Bad Elf GPS Pro+ 2.1.50"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd http://bad-elf.com/xmlschemas http://bad-elf.com/xmlschemas/GpxExtensionsV1.xsd">
    <metadata>
    <extensions>
    <badelf:modelNickname>BE021</badelf:modelNickname>
    <badelf:modelSerialNumber>011215</badelf:modelSerialNumber>
    <badelf:modelName>Bad Elf GPS Pro+</badelf:modelName>
    <badelf:modelNumber>BE-GPS-2300</badelf:modelNumber>
    <badelf:modelHardwareRevision>8.0.0</badelf:modelHardwareRevision>
    <badelf:modelFirmwareRevision>2.1.50</badelf:modelFirmwareRevision>
    <badelf:elevationSource>gps</badelf:elevationSource>
    </extensions>
    </metadata>
    <wpt lat="50.650192" lon="-1.186149">
    <ele>10.3</ele>
    <time>2021-07-24T14:47:40Z</time>
    <name>START (2021-07-24T14:47:40Z)</name>
    </wpt>
    <trk>
    <name>2021-07-24T14:47:40Z</name>
    <trkseg>
    <trkpt lat="50.650192" lon="-1.186149"><ele>10.3</ele><time>2021-07-24T14:47:40Z</time><hdop>0.9</hdop><extensions><badelf:speed>0.00</badelf:speed><badelf:baroEle>52.4</badelf:baroEle><badelf:baroPress>1005.81</badelf:baroPress></extensions></trkpt>
    <trkpt lat="50.650195" lon="-1.186144"><ele>8.8</ele><time>2021-07-24T14:47:41Z</time><hdop>0.9</hdop><extensions><badelf:speed>0.00</badelf:speed><badelf:baroEle>52.4</badelf:baroEle><badelf:baroPress>1005.81</badelf:baroPress></extensions></trkpt>
    <trkpt lat="50.650193" lon="-1.186146"><ele>8.9</ele><time>2021-07-24T14:47:41Z</time><hdop>0.9</hdop><extensions><badelf:speed>0.00</badelf:speed><badelf:baroEle>52.4</badelf:baroEle><badelf:baroPress>1005.81</badelf:baroPress></extensions></trkpt>
    </trkseg>
    </trk>
    <wpt lat="50.650168" lon="-1.186119">
    <ele>13.4</ele>
    <time>2021-07-24T15:52:30Z</time>
    <name>END (2021-07-24T15:52:30Z)</name>
    </wpt>
    </gpx>

What I would like is to extract the badelf:modelNickname to a variable and then make a datatable with column headings "lat" "lon" "ele" "time" "hdop" "badelf:speed" and "badelf:baropressure"

Can someone help me do this in VB.Net please? Or even C# as I can probably translate.

If you want to do this without a library to help you extract the XML, you could use Dmitry Bychenko 's recommendation in this SO post that shows how to take a substring between two "words".

Since your parameters will always have the same name, you could just replace the words in his example with the beginnings and endings of each parameter line.

This is how you could extract your badelf:modelNickname to a variable.

As for your datatable, I'm not sure what you're filling it with but if you just need a datatable with those columns, you can do so like @RKK 's answer on this SO Post :

Dim dt As New DataTable

dt.Columns.Add("Id", GetType(Integer))
dt.Columns.Add("FirstName", GetType(String))
dt.Columns.Add("LastName", GetType(String))

dt.Rows.Add(1, "Test", "data")
dt.Rows.Add(15, "Robert", "Wich")
dt.Rows.Add(18, "Merry", "Cylon")
dt.Rows.Add(30, "Tim", "Burst")

Let me know if that gets you closer to your goal or not.

There aren't any open-source projects that already do this. So I can offer some good examples that you can consider and extract code from. Both will require some development.

Jim Jackson from Microsoft had a good blog post on March 11, 2009 which was loaded with good source code showing how to use LINQ, to query various elements from a GPX file.

GpxReader is an open-source library from DLC. It has some good examples for extracting elements from a gpx file. Comments at the bottom had some good ideas for next-steps.

You could use either of those to extract the elements that you want and insert them into a datatable, etc. The code examples are in C#, but can be converted to VB.NET with one of the popular (online) code converters. LINQ in VB.NET can be a little awkward sometimes. It depends on your skills/experience.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM