GpxParser 1.0.2

dotnet add package GpxParser --version 1.0.2
                    
NuGet\Install-Package GpxParser -Version 1.0.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="GpxParser" Version="1.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="GpxParser" Version="1.0.2" />
                    
Directory.Packages.props
<PackageReference Include="GpxParser" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add GpxParser --version 1.0.2
                    
#r "nuget: GpxParser, 1.0.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package GpxParser@1.0.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=GpxParser&version=1.0.2
                    
Install as a Cake Addin
#tool nuget:?package=GpxParser&version=1.0.2
                    
Install as a Cake Tool

GpxParser

A lightweight .NET library for parsing GPX (GPS Exchange Format) files, allowing flexible extraction of track points with customizable output.

Features

  • Parse GPX files to extract track points (latitude, longitude, time, and more).
  • Supports Base64-encoded or raw XML input.
  • Generic output for flexible data structures (tuples, custom classes, etc.).
  • Robust handling of XML namespaces and invalid data.
  • Culture-invariant parsing for consistent results across locales.
  • Minimal dependencies (uses only System.Xml.Linq).

Installation

Install the GpxParser NuGet package using the .NET CLI or Package Manager Console:

.NET CLI

dotnet add package GpxParser

Package Manager Console

Install-Package GpxParser

Alternatively, search for GpxParser in the Visual Studio NuGet Package Manager.

Usage

Basic Example

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}");
}

Advanced Example

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}");
}

API Reference

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)
  • Parameters:
    • 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.
  • Returns: A List<T> containing the extracted track points.
  • Throws:
    • ArgumentNullException: If input is null or empty.
    • ArgumentException: If the input is invalid (e.g., malformed XML or Base64).

Requirements

  • .NET 9.
  • No external dependencies beyond the .NET runtime.

Contributing

Contributions are welcome! Please submit issues or pull requests to the GitHub repository.

License

This library is licensed under the MIT License. See the LICENSE file for details.

Support

For questions or issues, please open an issue on the GitHub repository or contact the maintainer at [email@example.com].

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net9.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.2 250 5/18/2025
1.0.1 202 5/18/2025
1.0.0 186 5/11/2025