Kml Class

Reads and writes KML 2.2 (OGC 12-007r2), the format of Google Earth and Google My Maps.

public static class Kml

Remarks

The reader returns the Placemark elements of the Document and Folder elements at any depth, in document order. The name of the top-level Document or Folder becomes GeoFeatureCollection.Name, and the id attribute of a placemark becomes GeoFeature.Id. The properties are the name and the description of the placemark (the text of a CDATA section, so HTML is kept as it is), the Data and SchemaData/SimpleData items of its ExtendedData (the first one wins when a name repeats, and an element wins over a data item of the same name), and folder, the names of the folders that contain the placemark, separated by / (the top-level Document or Folder excluded, and the unnamed folders skipped). KML has no types: the values are strings, except the SimpleData items whose SimpleField in the Schema of the document is numeric (a Double) or bool (a Boolean). A Data item without a value is null.

The geometries are Point, LineString, LinearRing (a GeoLineString, as KML draws a ring alone as a line), Polygon, MultiGeometry (a GeoMultiPoint, GeoMultiLineString or GeoMultiPolygon when its items are all points, all lines or all polygons; otherwise, a GeoGeometryCollection), and gx:Track and gx:MultiTrack (lines through their gx:coord positions, the when elements becoming the GeoPosition.Time of the positions; a when without a time of day, such as a date alone, gives no time). The altitudes are kept as they are written, whatever the altitudeMode. The styles, the time primitives of the placemarks, the Model geometries, the overlays and the network links are ignored, and nothing is downloaded. The elements are matched by their local name, so KML 2.0, 2.1 and 2.2 and the Google extensions are read. A document type definition is rejected and no external entity is resolved.

The writer writes a KML 2.2 Document, named after GeoFeatureCollection.Name, with a Placemark per feature. Its id is GeoFeature.Id when it is a valid XML identifier (an NCName, so not a number); otherwise, the identifier is not written. The name and description come from the string properties named by KmlWriterOptions, and the other properties go in ExtendedData as Data items with their text, independent of the culture (the null values are skipped). The multi-geometries and the collections become MultiGeometry elements, the rings of the polygons are closed, and a geometry with altitudes gets the absolute altitude mode. A GeoLineString whose positions all have a time is a gx:Track, and a GeoMultiLineString of such lines a gx:MultiTrack; the times of the other positions and the measures are not written.

Methods

NameDescription
Format(GeoFeatureCollection, KmlWriterOptions?)Writes features as a KML document.
Parse(string)Reads the placemarks of a KML document.
Read(Stream)Reads the placemarks of a KML document.
Write(GeoFeatureCollection, Stream, KmlWriterOptions?)Writes features as a KML document, encoded in UTF-8 without a byte order mark.

Format(GeoFeatureCollection, KmlWriterOptions?)

Kml.cs

Writes features as a KML document.

public static string Format(GeoFeatureCollection features, KmlWriterOptions? options = null)

Parameters

features GeoFeatureCollection
The features.
options KmlWriterOptions?
The options, or null for the defaults.

Returns

string The KML text.

Exceptions

ArgumentException
A position is not finite, or a text contains a character that XML cannot represent.

Parse(string)

Kml.cs

Reads the placemarks of a KML document.

public static GeoFeatureCollection Parse(string kml)

Parameters

kml string
The KML text.

Returns

GeoFeatureCollection The placemarks.

Exceptions

FormatException
The text is not well-formed XML or not KML.

Read(Stream)

Kml.cs

Reads the placemarks of a KML document.

public static GeoFeatureCollection Read(Stream stream)

Parameters

stream Stream
The KML document. Its encoding is read from its XML declaration. It is left open.

Returns

GeoFeatureCollection The placemarks.

Exceptions

FormatException
The document is not well-formed XML or not KML.

Write(GeoFeatureCollection, Stream, KmlWriterOptions?)

Kml.cs

Writes features as a KML document, encoded in UTF-8 without a byte order mark.

public static void Write(GeoFeatureCollection features, Stream stream, KmlWriterOptions? options = null)

Parameters

features GeoFeatureCollection
The features.
stream Stream
The stream to write to. It is left open.
options KmlWriterOptions?
The options, or null for the defaults.

Exceptions

ArgumentException
A position is not finite, or a text contains a character that XML cannot represent.