我正在编写自己的 C# 自定义地图导航程序。我在用打开街道地图用于地图数据。它是一个包含节点和方法的大型 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 文件包含a lot节点和方式,所以它不仅仅是一个小文件。下面的代码示例中的 XML 文件有 500K 行。
My Code
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 linq
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml;
using System.Xml.Linq;
using System.IO;
namespace ConsoleApplication31
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument xdoc = XDocument.Load(FILENAME);
var results = xdoc.Elements("root").Select(x => new
{
ns = x.Descendants("n").Select(y => new {
id = (long)y.Attribute("id"),
l = (double)y.Attribute("l"),
b = (double)y.Attribute("b")
}).ToList(),
ws = x.Descendants("w").Select(y => new {
id = (long)y.Attribute("id"),
rfs= y.Descendants("nd").Select(z => (long)z.Attribute("rf")).ToList()
}).ToList()
}).FirstOrDefault();
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)