forked from SharpMap/SharpMap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReadXmlExample.cs
More file actions
85 lines (74 loc) · 3.37 KB
/
ReadXmlExample.cs
File metadata and controls
85 lines (74 loc) · 3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
namespace ExampleCodeSnippets
{
public class ReadXmlExample
{
/// <summary>
/// Creates an enumeration of <see cref="GeoAPI.Geometries.Coordinate"/>s from an xml string
/// </summary>
/// <param name="factory"></param>
/// <param name="xml">the xml string</param>
/// <returns>Coordinates</returns>
public static System.Collections.Generic.IEnumerable<GeoAPI.Geometries.IGeometry> PointsFromXml(
GeoAPI.Geometries.IGeometryFactory factory,
System.IO.Stream xml)
{
foreach (var coordinate in CoordinatesFromXml(xml))
yield return factory.CreatePoint(coordinate);
}
/// <summary>
/// Creates an enumeration of <see cref="GeoAPI.Geometries.Coordinate"/>s from an xml string
/// </summary>
/// <param name="xml">the xml string</param>
/// <returns>Coordinates</returns>
public static System.Collections.Generic.IEnumerable<GeoAPI.Geometries.Coordinate> CoordinatesFromXml(System.IO.Stream xml)
{
var reader = System.Xml.XmlReader.Create(xml);
var doc = System.Xml.Linq.XDocument.Load(reader);
if (doc.Root == null) yield break;
var ptsName = System.Xml.Linq.XName.Get("Points");
var ptName = System.Xml.Linq.XName.Get("Point");
var xName = System.Xml.Linq.XName.Get("X");
var yName = System.Xml.Linq.XName.Get("Y");
foreach (var ptsElement in doc.Root.Elements(ptsName))
{
foreach (var ptElement in ptsElement.Elements(ptName))
{
var element = ptElement.Element(xName);
if (element == null || element.IsEmpty) continue;
var x = double.Parse(element.Value,
System.Globalization.NumberFormatInfo.InvariantInfo);
element = ptElement.Element(yName);
if (element == null || element.IsEmpty) continue;
var y = double.Parse(element.Value,
System.Globalization.NumberFormatInfo.InvariantInfo);
yield return new GeoAPI.Geometries.Coordinate(x, y);
}
}
}
[NUnit.Framework.Test]
public void TestXml1()
{
var xml = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
<root>
<Points>
<Point>
<X>13457786.5961983</X>
<Y>1629064.58490612</Y>
</Point>
</Points>
</root>";
var xmlFileName = System.IO.Path.ChangeExtension(System.IO.Path.GetTempFileName(), "xml");
using (var sw = new System.IO.StreamWriter(System.IO.File.OpenWrite(xmlFileName)))
sw.Write(xml);
var factory = new NetTopologySuite.Geometries.GeometryFactory();
SharpMap.Data.Providers.FeatureProvider p = null;
using (var fs = System.IO.File.OpenRead(xmlFileName))
{
p = new SharpMap.Data.Providers.FeatureProvider(PointsFromXml(factory, fs));
NUnit.Framework.Assert.IsNotNull(p);
NUnit.Framework.Assert.AreEqual(1, p.Features.Count);
}
System.IO.File.Delete(xmlFileName);
}
}
}