private KmlFile LoadFile(string path) { try { byte[] data = this.fileResolver.ReadFile(path); using (var stream = new MemoryStream(data, false)) { if (path.EndsWith(".kml", StringComparison.OrdinalIgnoreCase)) { return KmlFile.Load(stream); } if (path.EndsWith(".kmz", StringComparison.OrdinalIgnoreCase)) { if (this.fileResolver.SupportsKmz) { return this.fileResolver.ExtractDefaultKmlFileFromKmzArchive(stream); } } } } catch (IOException) { // Silently fail } return null; }
/// <summary> /// Reads a Kml file or the default Kml file from a Kmz archive. /// </summary> /// <param name="path">The path of the file.</param> /// <returns>A KmlFile on success, or null on an error.</returns> public static KmlFile ReadFile(string path) { try { byte[] data = ReadBytes(new Uri(path, UriKind.RelativeOrAbsolute)); using (var stream = new MemoryStream(data, false)) { string extension = Path.GetExtension(path); if (string.Equals(extension, ".kml", StringComparison.OrdinalIgnoreCase)) { return(KmlFile.Load(stream)); } if (string.Equals(extension, ".kmz", StringComparison.OrdinalIgnoreCase)) { using (var kmz = KmzFile.Open(stream)) { return(KmlFile.LoadFromKmz(kmz)); } } } } catch (IOException) { // Silently fail } return(null); }
/// <summary> /// Loads a default <see cref="KmlFile"/> inside this archive. /// </summary> /// <returns> /// A KmlFile representing the default KML file in the specified KMZ archive /// or null if no KML data was found. /// </returns> /// <remarks> /// This method checks for duplicate Id's in the file and throws an /// exception if duplicate Id's are found. To enable duplicate Id's /// use the <see cref="SharpKml.Base.Parser"/> class and pass the root /// element to <see cref="KmlFile.Create"/>. /// </remarks> /// <exception cref="ObjectDisposedException"> /// <see cref="Dispose"/> has been called on this instance. /// </exception> /// <exception cref="System.Xml.XmlException"> /// An error occurred while parsing the KML. /// </exception> public KmlFile GetDefaultKmlFile() { string kml = this.ReadKml(); if (kml != null) { using (var reader = new StringReader(kml)) { return(KmlFile.Load(reader)); } } return(null); }