Manipulate or Generate KML File in C#

KML Files

Aspose.GIS lets you open and read features from Keyhole Markup Language (KML) files.

Creating KML File

// For complete examples and data files, please go to https://github.com/aspose-gis/Aspose.GIS-for-.NET
using (var layer = Drivers.Kml.CreateLayer(dataDir + "Kml_File_out.kml"))
{
layer.Attributes.Add(new FeatureAttribute("string_data", AttributeDataType.String));
layer.Attributes.Add(new FeatureAttribute("int_data", AttributeDataType.Integer));
layer.Attributes.Add(new FeatureAttribute("bool_data", AttributeDataType.Boolean));
layer.Attributes.Add(new FeatureAttribute("float_data", AttributeDataType.Double));
Feature feature = layer.ConstructFeature();
feature.SetValue("string_data", "string value");
feature.SetValue("int_data", 10);
feature.SetValue("bool_data", true);
feature.SetValue("float_data", 3.14);
feature.Geometry = new LineString(new[] { new Point(0, 0), new Point(1, 1) });
layer.Add(feature);
Feature feature2 = layer.ConstructFeature();
feature2.SetValue("string_data", "string value2");
feature2.SetValue("int_data", 100);
feature2.SetValue("bool_data", false);
feature2.SetValue("float_data", 3.1415);
feature2.Geometry = Geometry.Null;
layer.Add(feature2);
}

Reading Features from KML File

// For complete examples and data files, please go to https://github.com/aspose-gis/Aspose.GIS-for-.NET
using (var layer = Drivers.Kml.OpenLayer(dataDir + "kml_file.kml"))
{
// get feratures count
int count = layer.Count;
// get feature at index 1
Feature featureAtIndex1 = layer[0];
Console.WriteLine(featureAtIndex1.GetValue<string>("string_data"));
Feature featureAtIndex2 = layer[1];
Console.WriteLine(featureAtIndex2.GetValue<string>("string_data"));
}

Reading Features from Invalid KML File

// For complete examples and data files, please go to https://github.com/aspose-gis/Aspose.GIS-for-.NET
var path = dataDir + "kml_invalid_chars.kml";
using (var layer = Drivers.Kml.OpenLayer(path, new KmlOptions() { SymbolToReplaceInvalidChars = '_' } ))
{
foreach (var feature in layer)
{
Console.WriteLine(feature.GetValue("name"));
Console.WriteLine(feature.GetValue("description"));
}
}

Export style properties to KML

// For complete examples and data files, please go to https://github.com/aspose-gis/Aspose.GIS-for-.NET
using (var layer = Drivers.Kml.CreateLayer(dataDir + "Kml_Styles_out.kml"))
{
var style = new KmlFeatureStyle
{
Line = new KmlLineStyle() { Width = 2.0 },
Polygon = new KmlPolygonStyle() { Outline = false },
Icon = new KmlIconStyle() { Resource = new KmlIconResource() { Href = "url" } },
Label = new KmlLabelStyle() { Color = Color.Green },
Balloon = new KmlBalloonStyle() { BackgroundColor = Color.Aqua, Text = "Example" },
List = new KmlListStyle() { ItemType = KmlItemTypes.RadioFolder },
};
Feature feature = layer.ConstructFeature();
feature.Geometry = new LineString(new[] { new Point(0, 0), new Point(1, 1) });
layer.Add(feature, style);
Feature feature2 = layer.ConstructFeature();
feature2.Geometry = new Point(5, 5);
layer.Add(feature2, style);
}