使用C#和LINQ生成带有递归函数的KML文件

时间:2012-10-12 15:10:11

标签: c# xml kml

我正在尝试在C#中动态创建KML文件。我写了一个递归函数来做到这一点。但是输出的结果有点问题。问题是关闭所有地标的标签的位置。我真的很困惑。请告诉我在递归函数中我犯了什么错误???

我的代码:

private void xmlBuild()
    {
XDocument doc = new XDocument(
        new XDeclaration("1.0", "utf-8", ""),
        new XComment("This is comment by me"),
        new XElement(ns+"kml", 
        new XElement(ns+"Document", rec_build())));      
        doc.Save(Server.MapPath(@"~\App_Data\markers2.xml"));
}
private XElement rec_build()
    {
        if (iteration != 0)
        {
            iteration -= 1;
            return final_rec = new XElement(ns + "Placemark",
                    new XAttribute("id", "1"),
                    new XElement(ns + "title", "something"),
                    new XElement(ns + "description", "something"),
                    new XElement(ns + "LookAt",
                    new XElement(ns + "Longitude", "49.69"),
                    new XElement(ns + "Latitude", "32.345")), new XElement(ns + "Point", new XElement(ns + "coordinates", "49.69,32.345,0")),rec_build());
        }
        else
        {
            return null;
        }
    }

这是迭代值2的输出:(请注意文件末尾的地标id = 1的结束标记。它应该在地标id = 2的起始标记之前!

<?xml version="1.0" encoding="utf-8"?>
<!--This is comment by me-->
<kml xmlns="http://earth.google.com/kml/2.2">
  <Document>
    <Placemark id="1">
      <title>something</title>
      <description>something</description>
      <LookAt>
        <Longitude>49.69</Longitude>
        <Latitude>32.345</Latitude>
      </LookAt>
      <Point>
        <coordinates>49.69,32.345,0</coordinates>
      </Point>
      <Placemark id="1">
        <title>something</title>
        <description>something</description>
        <LookAt>
          <Longitude>49.69</Longitude>
          <Latitude>32.345</Latitude>
        </LookAt>
        <Point>
          <coordinates>49.69,32.345,0</coordinates>
        </Point>
      </Placemark>
    </Placemark>
  </Document>
</kml>

2 个答案:

答案 0 :(得分:1)

所以问题是每次递归时,都要将元素添加到新创建的项目中。似乎循环可以更好地工作。

基本上代码是这样做的:

设置kml外围 第一次调用并将元素(元素1)添加到kml outerboy 第二次调用添加元素(元素2)到(元素1) 第3次调用将元素(元素3)添加到(元素2)。

如果你想做一个递归方法而不是循环机制,请传入对外部kml的引用。

如果这正是它的工作方式,那么递归会更加混乱

(对不起,如果我有一个额外的或缺少的括号,逗号或其他项目。我没有安装VS)

循环:

private void xmlBuild()
{
    XElement documentElement = new XElement(ns + "Document");

    for (int i = 0; i < 2; i++)
    {
        documentElement.Add(rec_build());
    }

    XDocument doc = new XDocument(
        new XDeclaration("1.0", "utf-8", ""),
        new XComment("This is comment by me"),
        new XElement(ns + "kml", documentElement));

    doc.Save(Server.MapPath(@"~\App_Data\markers2.xml"));
}

private XElement rec_build()
{

    return new XElement(ns + "Placemark",
           new XAttribute("id", "1"),
           new XElement(ns + "title", "something"),
           new XElement(ns + "description", "something"),
           new XElement(ns + "LookAt",
           new XElement(ns + "Longitude", "49.69"),
           new XElement(ns + "Latitude", "32.345")), 
           new XElement(ns + "Point", 
           new XElement(ns + "coordinates", "49.69,32.345,0")));
}

递归:

private void xmlBuild()
{
    XElement docElement = new XElement(ns+"Document");
    rec_build(docElement);
    XDocument doc = new XDocument(
    new XDeclaration("1.0", "utf-8", ""),
    new XComment("This is comment by me"),
    new XElement(ns+"kml", docElement)));      
    doc.Save(Server.MapPath(@"~\App_Data\markers2.xml"));
}
private XElement rec_build(XElement doc)
{
    if (iteration != 0)
    {
        iteration -= 1;
        doc.Add(new XElement(ns + "Placemark",
                new XAttribute("id", "1"),
                new XElement(ns + "title", "something"),
                new XElement(ns + "description", "something"),
                new XElement(ns + "LookAt",
                new XElement(ns + "Longitude", "49.69"),
                new XElement(ns + "Latitude", "32.345")), 
                new XElement(ns + "Point", new XElement(ns + "coordinates", "49.69,32.345,0")));
        return recBuild(doc);
    }
    else
    {
        return null;
    }
}

答案 1 :(得分:0)

您正在添加递归构建元素作为Placemark的子项,而不是Document。这应该可以解决问题:

    private void xmlBuild()
    {
        XElement docElement = new XElement(ns + "Document");
        XDocument doc = new XDocument(
                new XDeclaration("1.0", "utf-8", ""),
                new XComment("This is comment by me"),
                new XElement(ns + "kml", docElement));
        rec_build(docElement);
        doc.Save(Server.MapPath(@"~\App_Data\markers2.xml"));
    }

    private XElement rec_build(XElement docElement)
    {
        if (iteration != 0)
        {
            iteration -= 1;
            return final_rec = new XElement(ns + "Placemark",
                    new XAttribute("id", "1"),
                    new XElement(ns + "title", "something"),
                    new XElement(ns + "description", "something"),
                    new XElement(ns + "LookAt",
                    new XElement(ns + "Longitude", "49.69"),
                    new XElement(ns + "Latitude", "32.345")), 
                    new XElement(ns + "Point", new XElement(ns + "coordinates", "49.69,32.345,0")));
            docElement.Add(final_rec);
            rec_build(docElement);
        }
        else
            return null;
    }