我希望我在这里不仅仅是脑死亡,而是试图在.NET中创建我自己的KML类,并在导出时使用.NET序列化来实际生成XML。当涉及到位置标记时,我被这一部分卡住了。根据谷歌的API,一个KML应该在文档容器的根上有占位符。所以,类似这样的事情:
<?xml version="1.0" encoding="utf-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<name>text.xml</name>
<open>1</open>
<Placemark id="PM1">
<name>PM1 Full Name</name>
<description>Full Description...</description>
<Point id="g0">
<altitudeMode>clampToGround</altitudeMode>
<extrude>1</extrude>
<coordinates>-74.001,40.001,0</coordinates>
</Point>
</Placemark>
<Placemark id="PM2">
<name>PM3 Full Name</name>
<description>Full Description...</description>
<Point id="g1">
<altitudeMode>clampToGround</altitudeMode>
<extrude>1</extrude>
<coordinates>-74.000,40.000,0</coordinates>
</Point>
</Placemark>
</Document>
</kml>
请注意,占位符位于Document的根,而不在另一个名为“占位符”之类的元素中。那么如何在.NET中实现串行化呢。我创造了这样的东西:
public class Document
{
[XmlElement("name")]
public string Name { set; get; }
[XmlElement("open")]
public int Open { set; get; }
//This will Serialize to a container <Placemarks>...</Placemarks>
public List<Placemark> Placemarks { set; get; }
}
public class Placemark
{
public Placemark() { }
public Placemark(string name, string desc)
{
Name = name;
Description = desc;
}
[XmlElement("name")]
public string Name { set; get; }
[XmlElement("description")]
public string Description { set; get; }
}
但它产生了额外的元素<占位符></占位符>。
谢谢
为了回应评论,请在此处查看此示例代码:
http://ideone.com/pNdOOh
所以这就是代码输出的内容:
<?xml version="1.0" encoding="utf-8"?>
<Document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<name>Test.xml</name>
<open>0</open>
<Placemarks>
<Placemark>
<name>Mark0</name>
<description>What I am...</description>
</Placemark>
<Placemark>
<name>Mark1</name>
<description>What I am...</description>
</Placemark>
<Placemark>
<name>Mark2</name>
<description>What I am...</description>
</Placemark>
</Placemarks>
</Document>