Adding ExtendedData

Jun 1, 2014 at 1:55 AM
Edited Jun 1, 2014 at 1:56 AM
I'm trying to add extended data to a placemark and am apparently not doing something correctly as VS tells me that place.ExtendedData is null. Here's the portion of code that deals with creating a placemark and adding extendeddata:
foreach (GISAddress address in unAdds)
    Double dblLat = Double.Parse(;
    Double dblLng = Double.Parse(address.lng.ToString());

    Placemark place = new Placemark();
    place.Geometry = new Point() { Coordinate = new SharpKml.Base.Vector(dblLat, dblLng) };
    place.Name = address.address1 != null ? address.address1.ToString() : "";

    Data damage = new Data();
    damage.Name = "damage";
    damage.DisplayName = "Total Damage";
    damage.Value = address.damage.ToString();
Jun 1, 2014 at 4:35 PM
Edited Jun 1, 2014 at 4:35 PM
You should be able to assign to the ExtendedData property, so the following should work:
damage.Value = address.damage.ToString();

place.ExtendedData = new ExtendedData();

Jun 1, 2014 at 7:03 PM
Thanks Sam, that did it. The KML reference identifies that there is a potential to add multiple data items under extended data, but data in sharpkml appears to not be a collection. Is it possible to add multiple data items?
Jun 1, 2014 at 10:02 PM
As far as I can tell you can only add a single ExtendedData to a Feature, however, that ExtendedData can contain multiple pieces of data (which I think is what you're saying) - you should be able to add additional pieces of data via the AddData and/or AddSchemaData methods of the ExtendedData object.

Hope it helps.
Jun 1, 2014 at 10:09 PM
Edited Jun 1, 2014 at 10:11 PM
Thanks Sam. Yes, you are correct in the fact that only one extendeddata may be added to a feature,

However, the specs show that you can potentially have multiple name/value nodes under an ExtendedData node, which is what I'm looking for. It does not appear that this is implemented in sharpkml. Check Elements Specific to ExtendedData. The first example in the specification page is exactly what I'd like to do, to add "dataname" nodes for 3 nodes - let's call them damage, structureType, damageValue. I believe it may be just a slight modification of the code to support Data as a list rather than its current implementation. Would you agree?
Jun 2, 2014 at 9:45 AM
Initially when I saw the third type (namespace_prefix:other) I assumed this was basically saying that the KML parser must support additional elements it doesn't understand and make sure they get written back to the file (which it does).

However, reading the specification again and it mentions Allow any elements from a namespace other than this schema's namespace (lax validation) - that's going to require some additional work in the internals of the library to expose those, which I'll start working on when I have some free time.

As a workaround in the meantime you could use a Data object with the Name set to damage/structureType/etc and then the Value as required. I think this has the advantage that Google Earth will display the name/value pairs inside a balloon for the placemark (though I'm not 100% sure on that).
Jun 2, 2014 at 2:03 PM
Thanks Sam. If I'm using it correctly, the ExtendedData only allows for one name/value pair rather than many. Can you confirm?
Jun 3, 2014 at 6:35 AM
The OGC spec states for ExtendedData that it supports multiple Data, SchemaData and other nodes (§9.2.1):
<kml:Data> ... </kml:Data> [0..*]
<kml:SchemaData> ... </kml:SchemaData> [0..*]
Allow any elements from a namespace other than this schema's namespace (lax validation). [0..*]
Also worth noting that §9.3.2 mentions about the Data node:
The value of the name attribute shall be unique within the context of its parent kml:ExtendedData element.
So you can have multiple name/value pairs inside the ExtendedData, however, they must all have a unique name.
Jun 3, 2014 at 12:56 PM
Right, I believe we have previously established that fact. My reference is to sharpkml - I can't seem to add multiple name / value pairs. It only keeps the last one added, so I just want to confirm that sharpkml can indeed have multiples, and if so, how do we implement that?
Jun 4, 2014 at 7:06 PM
I've just tried the following code and it seems to produce the correct output for me:
public static void Main(string[] args)
    var extendedData = new ExtendedData();
    extendedData.AddData(new Data { Name = "Item1", Value = "Value1" });
    extendedData.AddData(new Data { Name = "Item2", Value = "Value2" });

    var placemark = new Placemark();
    placemark.ExtendedData = extendedData;

    var kml = new Kml();
    kml.Feature = placemark;

    var serializer = new Serializer();
This produces the following output, which has two name/value pairs as expected:
<?xml version="1.0" encoding="utf-8"?>
<kml xmlns="">
      <Data name="Item1">
      <Data name="Item2">
If you're still not getting the expected output could you post a short code snippet that reproduces the problem and I'll take a look at it.

Jun 5, 2014 at 2:15 AM
Yep, that works! Ugh, my brain!!!