public void TestReadKmlDoesNotReturnTheBOM() { const string ExampleString = "Example string with no Byte Order Mark."; KmzFile kmz = KmzFile.Create(KmlFile.Create(new Kml(), duplicates: false)); byte[] data = Encoding.UTF8.GetBytes(ExampleString); byte[] bom = Encoding.UTF8.GetPreamble(); kmz.UpdateFile("doc.kml", bom.Concat(data).ToArray()); string result = kmz.ReadKml(); Assert.That(result, Is.EqualTo(ExampleString)); }
public static KmlFile OpenFile(String filePath) { string fileExtension = System.IO.Path.GetExtension(filePath); using (FileStream fileStream = File.OpenRead(filePath)) { if (fileExtension.Equals(".kmz", StringComparison.OrdinalIgnoreCase)) { KmzFile kmzFile = KmzFile.Open(fileStream); string kmlFileString = kmzFile.ReadKml(); using (StringReader stringReader = new StringReader(kmlFileString)) { return(KmlFile.Load(stringReader)); } } else { return(KmlFile.Load(fileStream)); } } }
private void OpenFileButton_Click(object sender, RoutedEventArgs e) { // Create an instance of the open file dialog box. OpenFileDialog openFileDialog1 = new OpenFileDialog(); // Set filter options and filter index. openFileDialog1.Filter = "Google Earth (.kml .kmz)|*.kml;*.kmz"; openFileDialog1.FilterIndex = 1; openFileDialog1.Multiselect = true; // Call the ShowDialog method to show the dialog box. bool?userClickedOK = openFileDialog1.ShowDialog(); // Process input if the user clicked OK. if (userClickedOK == true) { KmlFile kmlFile = null; string fileName = openFileDialog1.FileName; string fileExtension = System.IO.Path.GetExtension(fileName); if (fileExtension.Equals(".kmz", StringComparison.OrdinalIgnoreCase)) { try { using (Stream fileStream = File.OpenRead(fileName)) { KmzFile kmzFile = KmzFile.Open(fileStream); string kmlFileString = kmzFile.ReadKml(); using (StringReader stringReader = new StringReader(kmlFileString)) { kmlFile = KmlFile.Load(stringReader); } }; } catch (Exception ex) { MessageBox.Show(ex.Message, "Failed to open a KMZ file", MessageBoxButton.OK, MessageBoxImage.Error); return; } } else { try { using (Stream fileStream = File.OpenRead(fileName)) { kmlFile = KmlFile.Load(fileStream); }; } catch (Exception ex) { MessageBox.Show(ex.Message, "Failed to open a KML file", MessageBoxButton.OK, MessageBoxImage.Error); return; } } KmlTreeViewController kmlTreeViewController = new KmlTreeViewController(); kmlTreeViewController.SetTreeView(this.KmlItemsTreeView); kmlTreeViewController.SetKML(kmlFile); kmlTreeViewController.ProcessKml(); kmlTreeViewController.TreeViewSelectionChanged += c_TreeViewSelectionChanged; KmlSchemaNameComboBoxController kmlSchemaComboBoxController = new KmlSchemaNameComboBoxController(); kmlSchemaComboBoxController.SetComboBox(this.SchemaListComboBox); kmlSchemaComboBoxController.SetKML(kmlFile); kmlSchemaComboBoxController.ProcessKml(); kmlSchemaComboBoxController.ComboBoxSelectionChanged += PropertySchemaSelectionChanged; kmlSchemaListViewController.SetListView(this.SchemaListView); kmlFeatureSchemaListViewController.SetListView(this.PropertySchemaListView); } }