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

KmzFile ignores changes to KmlFile


When changing the elements of a KML file, the KmzFile ignores them. This is due to the fact that the KmlFile is saved to the zip entry when the KmzFile is created. The KmzFile should cache the KmlFile and then save it when the user calls Save.

Use case:
Need to add files to the KmzFile and also add the files as placemarks to the KmlFile

var folder = new Folder();
// Add placemarks to folder

var doc = new Document { Description = new Description { Text = "doc" } };
var kml = new Kml { Feature = doc };

var kmlFile = KmlFile.Create(kml, false);
var kmzFile = KmzFile.Create(kmlFile);

// Loop through the attachments to add to the kmz and add as placemarks
foreach (var attachment in GetAttachments())
    string attachmentName = Path.GetFileName(attachment.Path);
    string path = Path.Combine("files", attachmentName);
    using (var attachmentStream = File.OpenRead(attachment.Path))
        kmzFile.AddFile(path, attachmentStream);

    // add the attachment as a placemark to the kml
    string description = string.Format("<![CDATA[<img style=\"max-width:500px;\" src=\"{0}\">]]>", path);
    folder.AddFeature(new Placemark
        Name = attachmentName,
        Description = new Description { Text = description },
        Geometry = geometry
string directory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), @"\Export\");
using (var kmlStream = new FileStream(Path.Combine(directory, GetFileName() + ".kml"), FileMode.Create))
    // This DOES save the new placemarks

// saves the file attachments but does NOT save the new kml placemarks
Current work around is to loop through the attachments twice.


skendrot wrote Jul 20 at 8:48 PM

Consider the use case of adding Photo Overlays as well.