我想在C#中動態創建一個KML文件。我寫了一個遞歸函數來做到這一點。但是輸出的結果有點問題。問題是關閉所有地標標籤的位置。我很困惑。請告訴我在遞歸函數中我犯了什麼錯誤?用遞歸函數生成使用C#和LINQ的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>
我首先嚐試使用FOR循環來做,但更加困惑,所以我想出了使用遞歸的想法。你對使用循環有什麼建議? –
我去了你的建議(調用rec_function外kml),但沒有得到好的結果。你還有什麼建議? –
@AminM更新了我的回覆。它應該工作。我只是把它貫穿我的頭(沒有ide)。 –