【问题标题】:How can I get another XML namespace value?如何获取另一个 XML 命名空间值?
【发布时间】:2021-11-03 00:39:22
【问题描述】:

我是 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# xml gml


    【解决方案1】:

    请尝试以下方法。

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

    【讨论】:

      猜你喜欢
      • 2013-05-15
      • 2022-11-10
      • 1970-01-01
      • 1970-01-01
      • 2016-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多