【问题标题】:reading namespace kml error NullReferenceException c# [duplicate]读取命名空间kml错误NullReferenceException c# [重复]
【发布时间】:2013-12-16 11:46:04
【问题描述】:

我想在我的 c# 应用程序中解析 kml。

XmlDocument doc = new XmlDocument();
doc.Load(fileKml);
XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable);
ns.AddNamespace("x", "http://www.opengis.net/kml/2.2");
XmlNode nodeKmlns = doc.SelectSingleNode("/x:kml", ns); string sKmlns = nodeKmlns.InnerText;
XmlNode nodeName = doc.SelectSingleNode("GroundOverlay/name"); string sName = nodeName.InnerText;
XmlNode nodehref = doc.SelectSingleNode("GroundOverlay/Icon/href"); string shref = nodehref.InnerText;
XmlNode north = doc.SelectSingleNode("GroundOverlay/LatLonBox/north"); string snorth = north.InnerText; double yn = Convert.ToDouble(snorth);
XmlNode south = doc.SelectSingleNode("GroundOverlay/LatLonBox/south"); string ssouth = south.InnerText; double ys = Convert.ToDouble(ssouth);
XmlNode east = doc.SelectSingleNode("GroundOverlay/LatLonBox/east"); string seast = east.InnerText; double xe = Convert.ToDouble(seast);
XmlNode west = doc.SelectSingleNode("GroundOverlay/LatLonBox/west"); string swest = west.InnerText; double xw = Convert.ToDouble(swest);

这是我的 .kml

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
<GroundOverlay>
    <name>osm_bandung</name>
    <Icon>
        <href>files/osm_bandung.png</href>
        <viewBoundScale>0.75</viewBoundScale>
    </Icon>
    <LatLonBox>
        <north>-6.928631334672425</north>
        <south>-6.956054957857409</south>
        <east>107.6467976125619</east>
        <west>107.6030622981136</west>
    </LatLonBox>
</GroundOverlay>
</kml>

我使用 addNameSpace 但仍然出错 运行时,一行代码错误

XmlNode nodeName = doc.SelectSingleNode("GroundOverlay/name"); string sName = nodeName.InnerText;

错误是NullReferenceException Object reference not set to an instance of an object.

如何解决?

【问题讨论】:

  • 该节点还需要命名空间规范。您需要为您访问的每个节点添加命名空间。 XmlNode nodeName = doc.SelectSingleNode("GroundOverlay/name", ns);

标签: c# xml kml


【解决方案1】:

我建议你使用 LINQ to XML:

var xdoc = XDocument.Load("data.xml");
XNamespace ns = xdoc.Root.GetDefaultNamespace();
var overlay = xdoc.Root.Element(ns + "GroundOverlay");
var icon = overlay.Element(ns + "Icon");
var box = overlay.Element(ns + "LatLonBox");

var groundOverlay = new
{
    Name = (string)overlay.Element(ns + "name"),
    Icon = new
    {
        Href = (string)icon.Element(ns + "href"),
        ViewBoundScale = (double)icon.Element(ns + "viewBoundScale")
    },
    LatLonBox = new
    {
        North = (double)box.Element(ns + "north"),
        South = (double)box.Element(ns + "south"),
        East = (double)box.Element(ns + "east"),
        West = (double)box.Element(ns + "west")
    }
};

那么你可以简单地使用

groundOverlay.LatLonBox.East // 107.6467976125619

考虑也创建自定义类,而不是在这里使用匿名类型

【讨论】:

  • 我使用您的代码并添加double yn = groundOverlay.LatLonBox.North; double ys = groundOverlay.LatLonBox.South; double xe = groundOverlay.LatLonBox.East; double xw = groundOverlay.LatLonBox.West; string shref = groundOverlay.Icon.Href;,它可以工作,但是当命名空间为空时会出错。但谢谢。 @Sergey Berezovskiy。
  • @Alexbelek 你还有那个命名空间错误吗?
  • 没有。它有效,错误导致我删除了 kml 中的命名空间。 @Sergey Berezovskiy
【解决方案2】:

由于您在文档中至少指定了一个命名空间,因此所有节点都属于该命名空间。因此,访问节点时需要始终指定命名空间,就像访问nodeKmlns 节点时一样:

XmlDocument doc = new XmlDocument();
doc.Load(fileKml);
XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable);
ns.AddNamespace("x", "http://www.opengis.net/kml/2.2");
XmlNode nodeKmlns = doc.SelectSingleNode("/x:kml", ns); 
string sKmlns = nodeKmlns.InnerText;
XmlNode nodeName = doc.SelectSingleNode("GroundOverlay/name", ns); 
string sName = nodeName.InnerText;
XmlNode nodehref = doc.SelectSingleNode("GroundOverlay/Icon/href", ns); 
string shref = nodehref.InnerText;
XmlNode north = doc.SelectSingleNode("GroundOverlay/LatLonBox/north", ns); 
string snorth = north.InnerText; double yn = Convert.ToDouble(snorth);
XmlNode south = doc.SelectSingleNode("GroundOverlay/LatLonBox/south", ns); 
string ssouth = south.InnerText; double ys = Convert.ToDouble(ssouth);
XmlNode east = doc.SelectSingleNode("GroundOverlay/LatLonBox/east", ns); 
string seast = east.InnerText; double xe = Convert.ToDouble(seast);
XmlNode west = doc.SelectSingleNode("GroundOverlay/LatLonBox/west", ns); 
string swest = west.InnerText; double xw = Convert.ToDouble(swest);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-09
    • 2013-06-17
    • 1970-01-01
    • 1970-01-01
    • 2011-06-09
    • 1970-01-01
    • 1970-01-01
    • 2014-05-10
    相关资源
    最近更新 更多