0

i want to parse kml in my c# app.

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

and here is my .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>

i use addNameSpace but still error when running, the code error in line

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

the error is NullReferenceException Object reference not set to an instance of an object.

how to fix that?

Mizipzor
  • 45,705
  • 20
  • 92
  • 136
Abox LsrKdz
  • 1,035
  • 4
  • 20
  • 41
  • 1
    That node also requires a namespace specification. You need to add the namespace for each node that you access. `XmlNode nodeName = doc.SelectSingleNode("GroundOverlay/name", ns);` – Andrei V Dec 16 '13 at 11:47

2 Answers2

1

I suggest you to use 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")
    }
};

Then you can simply use

groundOverlay.LatLonBox.East // 107.6467976125619

Consider also creating custom classes instead of using anonymous types here

Sergey Berezovskiy
  • 215,927
  • 33
  • 392
  • 421
  • i use your code and i add `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;` and it works but error when the namespace is null. but thank you. @Sergey Berezovskiy . – Abox LsrKdz Dec 17 '13 at 03:33
  • @Alexbelek do you still have that namespace error? – Sergey Berezovskiy Dec 17 '13 at 06:48
  • no. it works, error cause i delete the namespace in kml. @Sergey Berezovskiy – Abox LsrKdz Dec 18 '13 at 09:09
0

Since you specify at least one namespace in your document, all the nodes belong to that namespace. Therefore, you need to always specify the namespace when accessing a node, just like you do when accessing the nodeKmlns node:

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);
Andrei V
  • 6,788
  • 6
  • 38
  • 58