增强XML解析复杂性 - c#XML循环

时间:2016-12-08 09:47:08

标签: c# xml openstreetmap

我正在编写自己的c#自定义地图导航程序。我正在使用 OpenStreetMaps 作为地图数据。它是一个包含节点和方式的大型XML文件。我写了一个转换器,它将XML文件从无用的垃圾中删除(如时间戳,用户等),这样文件就会变小。

现在,当我尝试遍历XML并将数据转换为C#对象Ways和Nodes时,我遇到了一个问题。我需要做很多循环,加载时间太长。

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:如果我要更新/编辑我的问题,请告诉我而不是立即贬低。

1 个答案:

答案 0 :(得分:0)

使用xml linq

example_2 = copy.deepcopy(example_1)
example_2.outa = example_1.outa
相关问题