Search code examples
c#xmlgml

How can I get another XML namespace value?


I am new to XML, trying to read the GML file and found out that you can use XDocuments to load the GML file.

If I use the namespaces separately it works.
Working code:

XDocument doc = XDocument.Load(_gmlFilePath);

XNamespace agiv = "http://www.agiv.be/agiv";
XNamespace gml = "http://www.opengis.net/gml";

var agivQuery = doc.Descendants(agiv + "CrabAdr").Select(e =>new {
  Id = (int) e.Element(agiv + "ID"),
  StraatId = (int) e.Element(agiv + "STRAATNMID"),
  StraatNm = (string) e.Element(agiv + "STRAATNM"),
  HuisNr = (string) e.Element(agiv + "HUISNR"),
  HuisNrLabel = (string) e.Element(agiv + "HNRLABEL"),
  Niscode = (int) e.Element(agiv + "NISCODE"),
  Gemeente = (string) e.Element(agiv + "GEMEENTE"),
  Postcode = (int) e.Element(agiv + "POSTCODE")
});

var coordQuery = doc.Descendants(gml + "coord").Select(e =>new {
  X = (decimal) e.Element(gml + "X"),
  Y = (decimal) e.Element(gml + "Y")
});
foreach(var c in agivQuery) {
  Console.WriteLine(c);
  Console.WriteLine("\n --- \n");
}

foreach (var c in coordQuery)
{
    Console.WriteLine(c);
}

I wanted everything in one variable (agivQuery) and tried this too.
Not working code:

var agivQuery = doc.Descendants(agiv + "CrabAdr").Select(e =>new {
  Id = (int) e.Element(agiv + "ID"),
  StraatId = (int) e.Element(agiv + "STRAATNMID"),
  StraatNm = (string) e.Element(agiv + "STRAATNM"),
  HuisNr = (string) e.Element(agiv + "HUISNR"),
  HuisNrLabel = (string) e.Element(agiv + "HNRLABEL"),
  Niscode = (int) e.Element(agiv + "NISCODE"),
  Gemeente = (string) e.Element(agiv + "GEMEENTE"),
  Postcode = (int) e.Element(agiv + "POSTCODE"),
  X = (decimal) e.Element(gml + "X"),
  Y = (decimal) e.Element(gml + "Y")
});
foreach(var c in agivQuery) {
  Console.WriteLine(c);
  Console.WriteLine("\n --- \n");
}

//foreach (var c in coordQuery)
//{
//    Console.WriteLine(c);
//}

image
This is 30 lines of the GML file:

<?xml version="1.0" encoding="utf-8"?>
<agiv:FeatureCollection xmlns:agiv="http://www.agiv.be/agiv" xmlns:gml="http://www.opengis.net/gml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="CrabAdr.xsd">
   <gml:boundedBy>
      <gml:Box srsName="EPSG:31370">
         <gml:coordinates />
      </gml:Box>
   </gml:boundedBy>
   <gml:featureMember>
      <agiv:CrabAdr>
         <agiv:ID>2000000022</agiv:ID>
         <agiv:STRAATNMID>37468</agiv:STRAATNMID>
         <agiv:STRAATNM>Vuggelberg</agiv:STRAATNM>
         <agiv:HUISNR>6</agiv:HUISNR>
         <agiv:APPTNR />
         <agiv:BUSNR />
         <agiv:HNRLABEL>6</agiv:HNRLABEL>
         <agiv:NISCODE>24134</agiv:NISCODE>
         <agiv:GEMEENTE>Scherpenheuvel-Zichem</agiv:GEMEENTE>
         <agiv:POSTCODE>3270</agiv:POSTCODE>
         <agiv:HERKOMST>afgeleidVanGebouw</agiv:HERKOMST>
         <gml:pointProperty>
            <gml:Point srsName="EPSG:31370" xmlns:gml="http://www.opengis.net/gml">
               <gml:coord>
                  <gml:X>190413.49</gml:X>
                  <gml:Y>183795.53</gml:Y>
               </gml:coord>
            </gml:Point>
         </gml:pointProperty>
      </agiv:CrabAdr>
   </gml:featureMember>

Solution

  • Please try the following.

    c#

    void Main()
    {
        const string _gmlFilePath = @"e:\temp\GML.xml";
        XDocument doc = XDocument.Load(_gmlFilePath);
    
        XNamespace agiv = doc.Root.GetNamespaceOfPrefix("agiv");
        XNamespace gml = doc.Root.GetNamespaceOfPrefix("gml");
    
        var agivQuery = doc.Descendants(agiv + "CrabAdr").Select(e => new
        {
            Id = (int)e.Element(agiv + "ID"),
            StraatId = (int)e.Element(agiv + "STRAATNMID"),
            StraatNm = (string)e.Element(agiv + "STRAATNM"),
            HuisNr = (string)e.Element(agiv + "HUISNR"),
            HuisNrLabel = (string)e.Element(agiv + "HNRLABEL"),
            Niscode = (int)e.Element(agiv + "NISCODE"),
            Gemeente = (string)e.Element(agiv + "GEMEENTE"),
            Postcode = (int)e.Element(agiv + "POSTCODE"),
            X = (decimal)e.Element(gml + "pointProperty")
                .Descendants(gml + "X").FirstOrDefault(),
            Y = (decimal)e.Element(gml + "pointProperty")
                .Descendants(gml + "Y").FirstOrDefault()
        });
        
        foreach (var c in agivQuery)
        {
            Console.WriteLine(c);
        }
    }