Search code examples
vb.netgml-geographic-markup-lan

What is the best way to parse GML in VB.Net


I'm looking for the best way to parse GML to return the spatial data. As example here's a GML file:

<?xml version="1.0" encoding="utf-8"?>
<gml:FeatureCollection xmlns:gml="http://www.opengis.net/gml"
    xmlns:xlink="http://www.w3.org/1999/xlink"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:onecallgml="http://www.pelicancorp.com/onecallgml"
    xsi:schemaLocation="http://www.pelicancorp.com/onecallgml http://www.pelicancorp.com/digsafe/onecallgml.xsd">
<gml:featureMember>
    <onecallgml:OneCallReferral gml:id="digsite">
    <onecallgml:LocationDetails>    
    <gml:surfaceProperty>
    <gml:Polygon srsName="EPSG:2193">
    <gml:exterior>
    <gml:LinearRing>
    <gml:posList>
        1563229.00057526 5179234.72234694 1563576.83066077 5179352.36361939 1563694.22647617 5179123.23451613 1563294.42782719 5179000.13697214 1563229.00057526 5179234.72234694
    </gml:posList>
    </gml:LinearRing>
    </gml:exterior>
    </gml:Polygon>
    </gml:surfaceProperty>
    </onecallgml:LocationDetails>
    </onecallgml:OneCallReferral>
</gml:featureMember>
</gml:FeatureCollection>

How do I iterate through each featureMember, and then its polygon(s) and then get the posList coordinates into an array?


Solution

  • When dealing with XML in VB.NET, I recommend using LINQ to XML. You will probably want to extract more information (e.g. something to tie back to the featureMember), but a simple example could be:

    ' You will need to import the XML namespace
    Imports <xmlns:gml = "http://www.opengis.net/gml">
    ...
    Dim xml As XElement = XElement.Parse(myGmlString) ' or some other method
    Dim polys = (
        From fm In xml...<gml:featureMember>
        From poly In fm...<gml:Polygon>
        Select New With {
            .Name = poly.@srsName,
            .Coords = (poly...<gml:posList>.Value.Trim() _
                .Split({" "}, StringSplitOptions.RemoveEmptyEntries) _
                .Select(Function(x) CDbl(x))).ToArray()
        }
    ).ToList()
    

    This will give you a List of anonymous types with the polygon name and the coordinates as an array of Double.