This project has moved and is read-only. For the latest updates, please go here.

Can't load simple kml file created by SharpKml

Dec 6, 2012 at 7:22 PM
Edited Dec 6, 2012 at 7:22 PM

Hello, I've got an issue with this library. I'm creating and saving a simple kml file with only one placemark (just like in the example). This works, and this is a sample generated kml file:

<?xml version="1.0" encoding="utf-8"?>
<Placemark xmlns="http://www.opengis.net/kml/2.2">
  <name>FASDSDF</name>
  <Point>
    <coordinates>-36.78772,-7.1336</coordinates>
  </Point>
</Placemark>

When I try to load the file with SharpKml later on, it returns an 'empty' document, that when I try to get its root element, it just returns null. Am I missing something?

Exact file loading routine:

public static Kml OpenFile(string filePath) {
    KmlFile file;
    Kml kml;
    try {
        var kFile = KmlFile.Load(filePath);
        kml = kFile.Root as Kml;
    } catch (Exception e) {
        var kFile = KmzFile.Open(filePath);
        kml = KmlFile.LoadFromKmz(kFile).Root as Kml;
    }
    return kml;
}

 In this case, kml is null.

Dec 6, 2012 at 7:42 PM

Well, I solved this issue. In case anyone needs it, this is what I had to do:
I took some code from http://sharpkml.codeplex.com/discussions/401626 (specifically the part about creating a Document):

This was the old kml generating issue:

public static KmlFile MakeSimpleKmlFile(double lat, double lng, string name) {
    Point point = new Point();
    point.Coordinate = new Vector(lat, lng);
    Placemark placemark = new Placemark();
    placemark.Geometry = point;
    placemark.Name = name;
    KmlFile kml = KmlFile.Create(placemark, false);
    return kml;
}

 Here's my modified, working version:

public static KmlFile MakeSimpleKmlFile(double lat, double lng, string name) {
    var document = new Document();
    document.Id = name;
    document.Name = "null";
    Point point = new Point();
    point.Coordinate = new Vector(lat, lng);
    Placemark placemark = new Placemark();
    placemark.Geometry = point;
    placemark.Name = name;
    document.AddFeature(placemark);
    var Mykml = new Kml();
    Mykml.Feature = document;
    KmlFile MykmlFile = KmlFile.Create(Mykml, true);
    return MykmlFile;
}

Loading the file after saving works now. Cheers!

Dec 7, 2012 at 10:58 AM

Glad you managed to find the solution and thanks for posting it here for others to see!