public LoadMap ConvertKmltoMap() { Parser parser = new Parser(); return null; }
public static void Run() { Console.WriteLine("Creating a point at 37.42052549 latitude and -122.0816695 longitude.\n"); // This will be used for the placemark Point point = new Point(); point.Coordinate = new Vector(37.42052549, -122.0816695); Placemark placemark = new Placemark(); placemark.Name = "Cool Statue"; placemark.Geometry = point; // This is the root element of the file Kml kml = new Kml(); kml.Feature = placemark; Serializer serializer = new Serializer(); serializer.Serialize(kml); Console.WriteLine(serializer.Xml); Console.WriteLine("\nReading Xml..."); Parser parser = new Parser(); parser.ParseString(serializer.Xml, true); kml = (Kml)parser.Root; placemark = (Placemark)kml.Feature; point = (Point)placemark.Geometry; Console.WriteLine("Latitude:{0} Longitude:{1}", point.Coordinate.Latitude, point.Coordinate.Longitude); }
public void TestAttributes() { const string Xml = @" <TestElement Attribute=""string"" EnumAtt=""random""> <WebAddress> http://www.example.com </WebAddress> <Int>1</Int> <OptionalInt> 2 </OptionalInt> <Enum>random</Enum> </TestElement>"; Parser parser = new Parser(); parser.ParseString(Xml, true); TestElement element = parser.Root as TestElement; Assert.That(element, Is.Not.Null); Assert.That(element.Attribute, Is.EqualTo("string")); Assert.That(element.Enum, Is.EqualTo(ColorMode.Random)); Assert.That(element.EnumAtt, Is.EqualTo(ColorMode.Random)); Assert.That(element.Int, Is.EqualTo(1)); Assert.That(element.OptionalInt, Is.EqualTo(2)); Assert.That(element.Uri, Is.EqualTo(new Uri("http://www.example.com"))); }
/// <summary> /// Initializes a new instance of the <see cref="LinkResolver"/> class. /// </summary> /// <param name="input">The input to parse.</param> /// <param name="duplicates">Allow duplicates or not.</param> public LinkResolver(TextReader input, bool duplicates) { this.duplicates = duplicates; Parser parser = new Parser(); parser.ElementAdded += this.OnElementAdded; parser.Parse(input); }
public override void Run() { const string PointKml = @"<Point> <coordinates>-90.86948943473118,48.25450093195546</coordinates> </Point>"; Parser parser = new Parser(); parser.ParseString(PointKml, namespaces: false); Assert.That(parser.Root, Is.Not.Null); }
public void TestParse() { // Checks that <Icon> is parsed as an IconStyle.IconLink Parser parser = new Parser(); parser.ParseString(@"<IconStyle><Icon><href>image.jpg</href></Icon></IconStyle>", false); var icon = parser.Root as IconStyle; Assert.That(icon, Is.Not.Null); Assert.That(icon.Icon, Is.Not.Null); Assert.That(icon.Icon.Href.OriginalString, Is.EqualTo("image.jpg")); }
public void TestParse() { const string xml = "<file xmlns=\"http://example.com\" xmlns:x=\"http://example.com\" att=\"value\">" + "<root>" + "<![CDATA[<>]]><x:child/>" + "</root>" + "</file>"; Parser parser = new Parser(); parser.ParseString(xml, true); TestClass root = parser.Root as TestClass; Assert.That(root, Is.Not.Null); Assert.That(root.Text, Is.EqualTo("<root><><x:child /></root>")); Assert.That(root.Att, Is.EqualTo("value")); }
public void TestParse() { // Taken from http://atompub.org/rfc4287.html Example 1.1 const string Sample = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "<feed xmlns=\"http://www.w3.org/2005/Atom\">" + " <title>Example Feed</title>" + " <link href=\"http://example.org/\"/>" + " <updated>2003-12-13T18:30:02Z</updated>" + " <author>" + " <name>John Doe</name>" + " </author>" + " <id>urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6</id>" + " <entry>" + " <title>Atom-Powered Robots Run Amok</title>" + " <link href=\"http://example.org/2003/12/13/atom03\"/>" + " <id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>" + " <updated>2003-12-13T18:30:02Z</updated>" + " <summary>Some text.</summary>" + " </entry>" + "</feed>"; Parser parser = new Parser(); parser.ParseString(Sample, true); var feed = parser.Root as Feed; Assert.That(feed, Is.Not.Null); Assert.That(feed.Title, Is.EqualTo("Example Feed")); this.AssertLink(feed.Links, "http://example.org/"); Assert.That(feed.Updated, Is.EqualTo("2003-12-13T18:30:02Z")); Assert.That(feed.Id, Is.EqualTo("urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6")); Entry entry = null; foreach (var e in feed.Entries) { Assert.That(entry, Is.Null); // Make sure we've only got one entry = e; } Assert.That(entry, Is.Not.Null); // Make sure we found one Assert.That(entry.Title, Is.EqualTo("Atom-Powered Robots Run Amok")); this.AssertLink(entry.Links, "http://example.org/2003/12/13/atom03"); Assert.That(entry.Id, Is.EqualTo("urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a")); Assert.That(entry.Updated, Is.EqualTo("2003-12-13T18:30:02Z")); Assert.That(entry.Summary, Is.EqualTo("Some text.")); }
public void TestAtom() { var parser = new Parser(); parser.ParseString("<feed xmlns='http://www.w3.org/2005/Atom'/>", true); var feed = parser.Root as SharpKml.Dom.Atom.Feed; Assert.That(feed, Is.Not.Null); parser.ParseString( "<atom:content xmlns:atom='http://www.w3.org/2005/Atom'" + " xmlns='http://www.opengis.net/kml/2.2'>" + "<Placemark id='pm0'/>" + "</atom:content>", true); var content = parser.Root as SharpKml.Dom.Atom.Content; Assert.That(content, Is.Not.Null); Assert.That(content.Orphans.Count(), Is.EqualTo(1)); var placemark = content.Orphans.ElementAt(0) as Placemark; Assert.That(placemark, Is.Not.Null); Assert.That(placemark.Id, Is.EqualTo("pm0")); }
public void TestParse() { Parser parser = new Parser(); // Try with empty state parser.ParseString(@"<ItemIcon xmlns=""http://www.opengis.net/kml/2.2"" />", true); ItemIcon item = parser.Root as ItemIcon; Assert.That(item, Is.Not.Null); Assert.That(item.State, Is.EqualTo(ItemIconStates.None)); // Try with more than one value parser.ParseString(@"<ItemIcon xmlns=""http://www.opengis.net/kml/2.2""><state>open error</state></ItemIcon>", true); Assert.That(((ItemIcon)parser.Root).State, Is.EqualTo(ItemIconStates.Open | ItemIconStates.Error)); parser.ParseString(@"<ItemIcon xmlns=""http://www.opengis.net/kml/2.2""><state>open open error</state></ItemIcon>", true); Assert.That(((ItemIcon)parser.Root).State, Is.EqualTo(ItemIconStates.Open | ItemIconStates.Error)); // Try with an empty/invalid value parser.ParseString(@"<ItemIcon xmlns=""http://www.opengis.net/kml/2.2""><state /></ItemIcon>", true); Assert.That(((ItemIcon)parser.Root).State, Is.EqualTo(ItemIconStates.None)); parser.ParseString(@"<ItemIcon xmlns=""http://www.opengis.net/kml/2.2""><state>unknown</state></ItemIcon>", true); Assert.That(((ItemIcon)parser.Root).State, Is.EqualTo(ItemIconStates.None)); }
public void TestSingleSimpleDelete() { const string DeleteXml = "<Update>" + "<targetHref/>" + "<Delete>" + "<Placemark targetId=\"p\"/>" + "</Delete>" + "</Update>"; var parser = new Parser(); parser.ParseString("<Folder><Placemark id=\"p\"/></Folder>", false); var file = KmlFile.Create(parser.Root, false); var target = parser.Root as Folder; Assert.That(target, Is.Not.Null); Assert.That(target.Features.Count(), Is.EqualTo(1)); parser.ParseString(DeleteXml, false); var update = parser.Root as Update; Assert.That(update, Is.Not.Null); update.Process(file); Assert.That(target.Features.Count(), Is.EqualTo(0)); Assert.That(file.FindObject("p"), Is.Null); // Make sure it was deleted from the KmlFile too. }
private static KmlFile CreateFile(string kml, bool duplicates) { var parser = new Parser(); parser.ParseString(kml, false); return KmlFile.Create(parser.Root, duplicates); }
public void TestSingleSimpleChange() { const string ChangeXml = "<Update>" + "<targetHref/>" + "<Change>" + "<Placemark targetId=\"p\">" + "<name>NEW NAME</name>" + "</Placemark>" + "</Change>" + "</Update>"; var parser = new Parser(); parser.ParseString("<Placemark id=\"p\"><name>hi</name></Placemark>", false); var file = KmlFile.Create(parser.Root, false); var target = file.FindObject("p") as Placemark; Assert.That(target, Is.Not.Null); Assert.That(target.Name, Is.EqualTo("hi")); parser.ParseString(ChangeXml, false); var update = parser.Root as Update; Assert.That(update, Is.Not.Null); // Verify the test XML update.Process(file); Assert.That(target.Name, Is.EqualTo("NEW NAME")); Assert.That(target.TargetId, Is.Null); }
public void TestSingleSimpleCreate() { const string CreateXml = "<Update>" + "<targetHref/>" + "<Create>" + "<Folder targetId=\"f\">" + "<Placemark id=\"px\">" + "<name>Update-Created Placemark</name>" + "<Point>" + "<coordinates>-11.11,22,22</coordinates>" + "</Point>" + "</Placemark>" + "</Folder>" + "</Create>" + "</Update>"; var parser = new Parser(); parser.ParseString("<Folder id=\"f\"/>", false); var file = KmlFile.Create(parser.Root, false); var target = parser.Root as Folder; Assert.That(target, Is.Not.Null); Assert.That(target.Features.Count(), Is.EqualTo(0)); parser.ParseString(CreateXml, false); var update = parser.Root as Update; Assert.That(update, Is.Not.Null); // Verify the test XML update.Process(file); Assert.That(target.Features.ElementAt(0).Name, Is.EqualTo("Update-Created Placemark")); Assert.That(target.TargetId, Is.Null); Assert.That(file.FindObject("px"), Is.Not.Null); // Make sure it was added to the KmlFile too. }
public void TestParseAltitudeMode() { const string LatLonAltBoxAbsolute = "<LatLonAltBox>" + "<north>2.5</north>" + "<south>1.25</south>" + "<east>1.25</east>" + "<west>0</west>" + "<minAltitude>101.101</minAltitude>" + "<maxAltitude>202.202</maxAltitude>" + "<altitudeMode>absolute</altitudeMode>" + "</LatLonAltBox>"; Parser parser = new Parser(); parser.ParseString(LatLonAltBoxAbsolute, false); Assert.That(parser.Root, Is.Not.Null); LatLonAltBox box = parser.Root as LatLonAltBox; Assert.That(box, Is.Not.Null); // Verify the proper values in the object model: Assert.That(box.MinimumAltitude, Is.EqualTo(101.101)); Assert.That(box.MaximumAltitude, Is.EqualTo(202.202)); Assert.That(box.AltitudeMode, Is.EqualTo(AltitudeMode.Absolute)); Assert.That(box.North, Is.EqualTo(2.5)); Assert.That(box.South, Is.EqualTo(1.25)); Assert.That(box.East, Is.EqualTo(1.25)); Assert.That(box.West, Is.EqualTo(0)); const string LatLonAltBoxClampToGround = "<LatLonAltBox>" + "<altitudeMode>clampToGround</altitudeMode>" + "</LatLonAltBox>"; parser.ParseString(LatLonAltBoxClampToGround, false); Assert.That(parser.Root, Is.Not.Null); box = parser.Root as LatLonAltBox; Assert.That(box, Is.Not.Null); Assert.That(box.North, Is.Null); Assert.That(box.South, Is.Null); Assert.That(box.East, Is.Null); Assert.That(box.West, Is.Null); Assert.That(box.MinimumAltitude, Is.Null); Assert.That(box.MaximumAltitude, Is.Null); Assert.That(box.GXAltitudeMode, Is.Null); Assert.That(box.AltitudeMode, Is.EqualTo(AltitudeMode.ClampToGround)); const string LatLonAltBoxRelativeToGround = "<LatLonAltBox>" + "<altitudeMode>relativeToGround</altitudeMode>" + "</LatLonAltBox>"; parser.ParseString(LatLonAltBoxRelativeToGround, false); box = (LatLonAltBox)parser.Root; Assert.That(box.AltitudeMode, Is.EqualTo(AltitudeMode.RelativeToGround)); const string LatLonAltBoxRelativeToSeaFloor = "<LatLonAltBox>" + "<gx:altitudeMode>relativeToSeaFloor</gx:altitudeMode>" + "</LatLonAltBox>"; parser.ParseString(LatLonAltBoxRelativeToSeaFloor, false); box = (LatLonAltBox)parser.Root; Assert.That(box.GXAltitudeMode, Is.EqualTo(SharpKml.Dom.GX.AltitudeMode.RelativeToSeafloor)); }
public void TestSerialize() { // This needs to be in this order const string TestKml = "<LatLonAltBox xmlns=\"http://www.opengis.net/kml/2.2\">" + "<north>2.5</north>" + "<west>0</west>" + "<minAltitude>101.101</minAltitude>" + "<altitudeMode>absolute</altitudeMode>" + "</LatLonAltBox>"; Parser parser = new Parser(); parser.ParseString(TestKml, true); Assert.That(parser.Root, Is.Not.Null); LatLonAltBox box = parser.Root as LatLonAltBox; Assert.That(box, Is.Not.Null); // Check it was parsed ok Assert.That(box.North, Is.EqualTo(2.5)); Assert.That(box.South, Is.Null); Assert.That(box.East, Is.Null); Assert.That(box.West, Is.EqualTo(0)); Assert.That(box.MinimumAltitude, Is.EqualTo(101.101)); Assert.That(box.MaximumAltitude, Is.Null); Assert.That(box.GXAltitudeMode, Is.Null); Assert.That(box.AltitudeMode, Is.EqualTo(AltitudeMode.Absolute)); Serializer serializer = new Serializer(); serializer.SerializeRaw(box); Assert.That(serializer.Xml, Is.EqualTo(TestKml)); }
public void TestValidKml() { const string Xml = "<kml xmlns=\"http://www.opengis.net/kml/2.2\">" + "<Placemark>" + "<name>a good Placemark</name>" + "<Point>" + "<coordinates>1,2,3</coordinates>" + "</Point>" + "</Placemark>" + "</kml>"; var parser = new Parser(); parser.ParseString(Xml, true); Assert.That(parser.Root, Is.Not.Null); Assert.That(parser.Root, Is.InstanceOf<Kml>()); // Test empty but valid Kml parser.ParseString("<kml />", false); Assert.That(parser.Root, Is.Not.Null); Assert.That(parser.Root, Is.InstanceOf<Kml>()); }
public void TestInvalidKml() { var parser = new Parser(); parser.ParseString("<gml><this>is not<kml/></this>is also not</gml>", false); Assert.That(parser.Root, Is.Null); parser.ParseString("<gml><Placemark><name>still not kml</name></Placemark></gml>", false); Assert.That(parser.Root, Is.Null); }
public void TestInvalidValuesDefault() { const string Xml = @" <TestElement> <Int>not a number</Int> <OptionalInt>not a number</OptionalInt> <Enum>not a value</Enum> </TestElement>"; Parser parser = new Parser(); parser.ParseString(Xml, true); TestElement element = parser.Root as TestElement; Assert.That(element, Is.Not.Null); Assert.That(element.Attribute, Is.Null); Assert.That(element.Enum, Is.EqualTo(ColorMode.Normal)); Assert.That(element.Int, Is.EqualTo(0)); Assert.That(element.OptionalInt, Is.Null); }
public void TestEmptyElement() { const string xml = @"<?xml version='1.0' encoding='UTF-8'?> <kml xmlns='http://www.opengis.net/kml/2.2'> <Document> <Snippet/> <name>My Document</name> <Placemark> <name>My Placemark</name> </Placemark> </Document> </kml>"; var parser = new Parser(); parser.ParseString(xml, true); Kml kml = parser.Root as Kml; Assert.That(kml, Is.Not.Null); Document document = kml.Feature as Document; Assert.That(document, Is.Not.Null); Assert.That(document.Name, Is.EqualTo("My Document")); Placemark placemark = document.Features.FirstOrDefault() as Placemark; Assert.That(placemark, Is.Not.Null); Assert.That(placemark.Name, Is.EqualTo("My Placemark")); }
public void TestCulturalSettings() { const string xml = "<DoubleElement><Double>12.34</Double></DoubleElement>"; var parser = new Parser(); var oldCulture = Thread.CurrentThread.CurrentCulture; try { Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture; parser.ParseString(xml, true); Assert.That(((DoubleElement)parser.Root).Double, Is.EqualTo(12.34)); Thread.CurrentThread.CurrentCulture = new CultureInfo("de"); parser.ParseString(xml, true); Assert.That(((DoubleElement)parser.Root).Double, Is.EqualTo(12.34)); } catch (NotSupportedException) // Mono under OS X doesn't like using "de" { throw new InconclusiveException("German culture not available."); } catch (ArgumentException) // Culture doesn't exist { throw new InconclusiveException("German culture not available."); } finally { Thread.CurrentThread.CurrentCulture = oldCulture; } }
public void TestSave() { // Create the Kml data const string Xml = "<Placemark xmlns='http://www.opengis.net/kml/2.2'><name>tmp kml</name></Placemark>"; var parser = new Parser(); parser.ParseString(Xml, true); var kml = KmlFile.Create(parser.Root, false); // This will be where we temporary save the archive to string tempFile = Path.GetTempFileName(); try { // Create and save the archive using (var file = KmzFile.Create(kml)) { file.Save(tempFile); } // Try to open the saved archive using (var file = KmzFile.Open(tempFile)) { // Make sure it's the same as what we saved parser.ParseString(file.ReadKml(), true); SampleData.CompareElements(kml.Root, parser.Root); } } finally { File.Delete(tempFile); } }
public void TestLegacyKml() { const string Xml = "<kml xmlns=\"http://earth.google.com/kml/2.2\">" + "<Placemark>" + "<name>My Placemark</name>" + "</Placemark>" + "</kml>"; var parser = new Parser(); parser.ParseString(Xml, false); Kml root = parser.Root as Kml; Assert.That(root, Is.Not.Null); // Make sure it didn't add the old namespace Assert.That(root.Namespaces.GetNamespacesInScope(XmlNamespaceScope.ExcludeXml), Is.Not.Contains(string.Empty)); // Make sure it serializes Serializer serializer = new Serializer(); Assert.That(() => serializer.Serialize(root), Throws.Nothing); Assert.That(serializer.Xml, Is.Not.Null.Or.Empty); }
public void TestChild() { const string ChildXml = "<ChildElement counter=\"1\" />"; const string TestXml = "<TestElement>" + "<Enum>normal</Enum>" + "<Int>10</Int>" + ChildXml + "</TestElement>"; Parser parser = new Parser(); parser.ParseString(ChildXml, true); Assert.That(parser.Root, Is.InstanceOf<ChildElement>()); parser.ParseString(TestXml, true); TestElement element = parser.Root as TestElement; Assert.That(element, Is.Not.Null); Assert.That(element.Int, Is.EqualTo(10)); Assert.That(element.Child.Counter, Is.EqualTo(1)); }
public void TestNamespaces() { const string withNs = @" <LatLonAltBox xmlns=""http://www.opengis.net/kml/2.2""> <gx:altitudeMode xmlns:gx=""http://www.google.com/kml/ext/2.2""> relativeToSeaFloor </gx:altitudeMode> </LatLonAltBox>"; const string withoutNs = @" <LatLonAltBox> <gx:altitudeMode> relativeToSeaFloor </gx:altitudeMode> </LatLonAltBox>"; // Test ParseString with a namespace but without namespace checking Parser parser = new Parser(); parser.ParseString(withNs, false); LatLonAltBox box = parser.Root as LatLonAltBox; Assert.That(box, Is.Not.Null); Assert.That(box.GXAltitudeMode, Is.EqualTo(SharpKml.Dom.GX.AltitudeMode.RelativeToSeafloor)); // Now without parser.ParseString(withoutNs, false); box = (LatLonAltBox)parser.Root; Assert.That(box.GXAltitudeMode, Is.EqualTo(SharpKml.Dom.GX.AltitudeMode.RelativeToSeafloor)); // Test ParseString with a namespace and with namespace checking parser.ParseString(withNs, true); box = parser.Root as LatLonAltBox; Assert.That(box, Is.Not.Null); Assert.That(box.GXAltitudeMode, Is.EqualTo(SharpKml.Dom.GX.AltitudeMode.RelativeToSeafloor)); // Now without, which shouldn't work as gx is unknown Assert.That(() => parser.ParseString(withoutNs, true), Throws.TypeOf<XmlException>()); }
private void BUT_loadkml_Click(object sender, EventArgs e) { OpenFileDialog fd = new OpenFileDialog(); fd.Filter = "Google Earth KML (*.kml)|*.kml"; fd.DefaultExt = ".kml"; DialogResult result = fd.ShowDialog(); string file = fd.FileName; if (file != "") { try { kmlpolygons.Polygons.Clear(); var parser = new SharpKml.Base.Parser(); parser.ElementAdded += new EventHandler<SharpKml.Base.ElementEventArgs>(parser_ElementAdded); parser.Parse(File.OpenRead(file)); } catch (Exception ex) { MessageBox.Show("Bad KML File :" + ex.ToString()); } } }
public void TestUnkownElement() { var parser = new Parser(); parser.ParseString("<kml><a>b<c></c></a></kml>", false); var kml = parser.Root as Kml; Assert.That(kml, Is.Not.Null); Assert.That(kml.Orphans.Count(), Is.EqualTo(1)); var unknown = kml.Orphans.ElementAt(0) as UnknownElement; Assert.That(unknown, Is.Not.Null); Assert.That(unknown.InnerText, Is.EqualTo("b")); // Other elements will be children }
public void TestMergeStyle() { const string SourceStyle = "<Style>" + " <IconStyle>" + " <scale>1.3</scale>" + " <Icon>" + " <href>cool.jpeg</href>" + " </Icon>" + " </IconStyle>" + "</Style>"; const string TargetStyle = "<Style>" + " <IconStyle>" + " <scale>1.5</scale>" + " <heading>123</heading>" + " </IconStyle>" + "</Style>"; Parser parser = new Parser(); parser.ParseString(SourceStyle, false); Style source = parser.Root as Style; parser.ParseString(TargetStyle, false); Style target = parser.Root as Style; target.Merge(source); // Make sure merge worked correctly and only affected the target Assert.That(source.Icon.Scale, Is.EqualTo(1.3)); Assert.That(target.Icon.Scale, Is.EqualTo(1.3)); Assert.That(source.Icon.Heading, Is.Null); Assert.That(target.Icon.Heading, Is.EqualTo(123)); Assert.That(source.Icon.Icon.Href, Is.EqualTo(new Uri("cool.jpeg", UriKind.Relative))); Assert.That(target.Icon.Icon.Href, Is.EqualTo(new Uri("cool.jpeg", UriKind.Relative))); }
public void TestSave() { // Create the Kml data const string Xml = "<Placemark xmlns='http://www.opengis.net/kml/2.2'><name>tmp kml</name></Placemark>"; var parser = new Parser(); parser.ParseString(Xml, true); var kml = KmlFile.Create(parser.Root, false); using (var stream = new MemoryStream()) { // Create and save the archive using (var file = KmzFile.Create(kml)) { file.Save(stream); } // Try to open the saved archive, rewinding the stream stream.Position = 0; using (var file = KmzFile.Open(stream)) { // Make sure it's the same as what we saved parser.ParseString(file.ReadKml(), true); SampleData.CompareElements(kml.Root, parser.Root); } } }