2013-12-16 60 views
0

我想在我的c#應用程序中解析kml。閱讀命名空間kml錯誤NullReferenceException c#

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.

如何解決這個問題?

+1

自定義類該節點還需要命名空間規範。您需要爲您訪問的每個節點添加名稱空間。 'XmlNode nodeName = doc.SelectSingleNode(「GroundOverlay/name」,ns);' –

回答

1

我建議你使用LINQ到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 

想想也創造而不是使用匿名類型這裏

+0

我使用你的代碼,我添加'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別列佐夫斯基。 –

+0

@Alexbelek你還有這個命名空間錯誤嗎? –

+0

沒有。它的工作原理,錯誤導致我刪除名稱空間在kml。 @Sergey別列佐夫斯基 –

0

由於您在文檔中指定了至少一個名稱空間,因此所有節點都屬於該名稱空間。因此,你需要訪問某個節點時始終指定命名空間,就像你做訪問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);