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

Posted

技术标签:

【中文标题】如何获取另一个 XML 命名空间值?【英文标题】:How can I get another XML namespace value? 【发布时间】: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>

【问题讨论】:

【参考方案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);
    

【讨论】:

以上是关于如何获取另一个 XML 命名空间值?的主要内容,如果未能解决你的问题,请参考以下文章

Linq to XML - 使用命名空间获取元素

如何在 CSS 选择器或 jQuery 中处理 XML 命名空间?

如何从 XML/MPD 文件中获取属性值?

如何使用 XPath 忽略命名空间

如何获取命名空间中的所有类?

如何加载指定路径的Logback.xml