如何获得另一个 XML 命名空间值?

How can I get another XML namespace value?

我是 XML 的新手,尝试阅读 the GML file 并发现您可以使用 XDocuments 加载 GML 文件。

如果我单独使用命名空间,它就可以工作。
工作代码:

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

我想要一个变量中的所有内容 (agivQuery) 也试过了。
无效代码:

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


这是 GML 文件的 30 行:

<?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>

请尝试以下操作。

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