【发布时间】:2017-04-23 12:11:17
【问题描述】:
我正在编写自己的 C# 自定义地图导航程序。我将 OpenStreetMaps 用于地图数据。这是一个包含节点和方式的大型 XML 文件。我写了一个转换器,从无用的垃圾(如时间戳、用户等)中去除 XML 文件,因此文件会变得更小。
现在,当我尝试遍历 XML 并将数据转换为 C# 对象方式和节点时,我遇到了一个问题。我需要做很多循环,以至于加载时间太长了。
XML 示例(w = 方式,n = 节点,rf = 对节点的引用):
<n id="2638006578" l="5.9295547" b="52.5619519" />
<n id="2638006579" l="5.9301973" b="52.5619526" />
<n id="2638006581" l="5.9303625" b="52.5619565" />
<n id="2638006583" l="5.9389539" b="52.5619577" />
<n id="2638006589" l="5.9386643" b="52.5619733" />
<n id="2638006590" l="5.9296231" b="52.5619760" />
<n id="2638006595" l="5.9358987" b="52.5619864" />
<n id="2638006596" l="5.9335913" b="52.5619865" />
<w id="453071384">
<nd rf="2638006581" />
<nd rf="2638006590" />
<nd rf="2638006596" />
<nd rf="2638006583" />
<nd rf="2638006578" />
</w>
<w id="453071385">
<nd rf="2638006596" />
<nd rf="2638006578" />
<nd rf="2638006581" />
<nd rf="2638006583" />
</w>
Way 节点包含对节点的引用,因此一个节点可以以多种方式存在(节点连接这些方式)。
节点包含经度和纬度(l = lon,b = lat)。
这些 XML 文件包含 很多 节点和路径,因此它不仅仅是一个小文件。下面代码示例中的 XML 文件有 500K 行。
我的代码
class Program {
static List<Way> ways = new List<Way>();
static List<Node> nodes = new List<Node>();
static void Main(String[] args) {
read();
}
public static void read() {
String xmlLoc = @"C:\Users\MyName\Desktop\result.xml";
long time = DateTime.Now.Ticks;
Parse(xmlLoc);
long time2 = DateTime.Now.Ticks;
Console.WriteLine("Done: {0} ms", (time2 - time) / 10000);
Console.WriteLine(" - Nodes Amount:" + nodes.Count());
Console.WriteLine(" - Ways Amount: " + ways.Count());
}
private static Node GetByRef(long reference) {
return nodes.First(x => x.ID == reference);
}
private static void Parse(string path) {
using (XmlReader reader = XmlReader.Create(path)) {
reader.MoveToContent();
while (reader.Read()) {
if (reader.NodeType == XmlNodeType.Element) {
XElement element = XElement.ReadFrom(reader) as XElement;
if ( element.Name.ToString().Equals("w")) {
Way w = new Way();
var name = element.Attribute("nm");
if (name != null) w.Name = name.Value;
var children = element.Descendants("nd");
foreach (XElement child in children) w.References.Add(long.Parse(child.Attribute("rf").Value));
ways.Add(w);
}else if (element.Name.ToString().Equals("n")) {
Node n = new Node();
n.ID = long.Parse(element.Attribute("id").Value);
n.Lon = double.Parse(element.Attribute("l").Value);
n.Lat = double.Parse(element.Attribute("b").Value);
nodes.Add(n);
}
}
}
}
}
}
class Way {
public List<long> References { get; private set; }
public long ID { get; set; }
public String Name { get; set; }
public bool OneWay { get; set; }
public Way() {
this.References = new List<long>();
}
}
class Node {
public long ID { get; set; }
public double Lat { get; set; }
public double Lon { get; set; }
}
目前Way类和Node类之间没有实际的关系。只有一个包含long 值的列表。我需要在类 Way 中添加一个带有节点的列表,但这需要我使用另一个(两个?)for/while 循环。我相信这意味着 O(N4),这很慢。
从技术上讲,我正在寻找更好的解决方案和方法,如果您有建议,我想听听!
提前致谢!
PS:如果我应该更新/编辑我的问题,请告诉我而不是立即投反对票。
【问题讨论】:
-
考虑内置 XML 反序列化,而不是编写自己的解析器。第一个例子:stackoverflow.com/questions/364253/…
-
真的有必要多次循环遍历 XML 文件吗?只做一次并存储以后需要的所有东西怎么样?
-
@Tomalak 我会调查的,谢谢!
-
@Tomalak 我写了一个基于反序列化的新问题,你可以看看吗?如果可能的话。 stackoverflow.com/questions/41039548/…
-
顺便说一句,本机反序列化比您的方法更快吗?
标签: c# xml openstreetmap