A library for Parsing GPX files
$ dotnet add package GpxParserA lightweight .NET library for parsing GPX (GPS Exchange Format) files, allowing flexible extraction of track points with customizable output.
System.Xml.Linq).Install the GpxParser NuGet package using the .NET CLI or Package Manager Console:
dotnet add package GpxParser
Install-Package GpxParser
Alternatively, search for GpxParser in the Visual Studio NuGet Package Manager.
Extract latitude, longitude, and time from a GPX file:
using System;
using System.Collections.Generic;
using System.Globalization;
string gpxContent = "BASE64_ENCODED_GPX_STRING"; // or raw GPX XML
var points = GpxParser.ExtractGpxTrackPoints(
gpxContent,
(trkpt, ns) =>
{
if (!decimal.TryParse(trkpt.Attribute("lat")?.Value, NumberStyles.Float, CultureInfo.InvariantCulture, out var lat) ||
!decimal.TryParse(trkpt.Attribute("lon")?.Value, NumberStyles.Float, CultureInfo.InvariantCulture, out var lon))
{
return null;
}
var timeStr = trkpt.Element(ns + "time")?.Value;
if (string.IsNullOrEmpty(timeStr) || !DateTime.TryParse(timeStr, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal, out var time))
{
return null;
}
return (Latitude: lat, Longitude: lon, Time: time);
},
isBase64: true // Set to false for raw XML
);
foreach (var point in points)
{
Console.WriteLine($"Lat: {point.Latitude}, Lon: {point.Longitude}, Time: {point.Time}");
}
Extract additional elements, such as elevation, into a custom object:
using System;
using System.Collections.Generic;
using System.Globalization;
string gpxContent = "<gpx>...</gpx>"; // Raw GPX XML
var points = GpxParser.ExtractGpxTrackPoints(
gpxContent,
(trkpt, ns) =>
{
if (!decimal.TryParse(trkpt.Attribute("lat")?.Value, NumberStyles.Float, CultureInfo.InvariantCulture, out var lat) ||
!decimal.TryParse(trkpt.Attribute("lon")?.Value, NumberStyles.Float, CultureInfo.InvariantCulture, out var lon))
{
return null;
}
var timeStr = trkpt.Element(ns + "time")?.Value;
if (string.IsNullOrEmpty(timeStr) || !DateTime.TryParse(timeStr, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal, out var time))
{
return null;
}
decimal? elevation = null;
var eleStr = trkpt.Element(ns + "ele")?.Value;
if (!string.IsNullOrEmpty(eleStr))
{
decimal.TryParse(eleStr, NumberStyles.Float, CultureInfo.InvariantCulture, out var ele);
elevation = ele;
}
return new
{
Latitude = lat,
Longitude = lon,
Time = time,
Elevation = elevation
};
},
isBase64: false
);
foreach (var point in points)
{
Console.WriteLine($"Lat: {point.Latitude}, Lon: {point.Longitude}, Time: {point.Time}, Elevation: {point.Elevation}");
}
ExtractGpxTrackPoints<T>Extracts track points from a GPX file or string.
public static List<T> ExtractGpxTrackPoints<T>(
string input,
Func<XElement, XNamespace, T?> pointExtractor,
bool isBase64 = false)input: The GPX content (Base64-encoded or raw XML string).pointExtractor: A function to extract data from each <trkpt> element, returning a user-defined type T or null to skip invalid points.isBase64: Set to true if the input is Base64-encoded; otherwise, false.List<T> containing the extracted track points.ArgumentNullException: If input is null or empty.ArgumentException: If the input is invalid (e.g., malformed XML or Base64).Contributions are welcome! Please submit issues or pull requests to the GitHub repository.
This library is licensed under the MIT License. See the LICENSE file for details.
For questions or issues, please open an issue on the GitHub repository or contact the maintainer at [email@example.com].