0

我希望我在这里不仅仅是脑死亡,而是我正在尝试在 .NET 中创建自己的 KML 类,并在导出时使用 .net 序列化来实际生成 XML。当涉及到地标时,我被困在这一部分。根据 Google 的 API,KML 应该在 Document Container 的根目录中包含 Placemarks。所以,像这样:

<?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 的根目录中,而不是位于另一个名为“Placemarks”之类的元素中。那么如何在带有序列化的.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; }
}

但它会生成额外的元素 <Placemarks>...</Placemarks>。

谢谢


只是为了回复评论,请在此处查看此示例代码:

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>
4

2 回答 2

1

只需使用XmlElement属性Placemarks

public class Document
{
    [XmlElement("name")]
    public string Name { set; get; }

    [XmlElement("open")]
    public int Open { set; get; }

    [XmlElement("Placemark")]
    public List<Placemark> Placemarks { set; get; }
}

编辑

并获得类似的 xml

  • 声明一个类kml

    [XmlRoot(Namespace="http://www.opengis.net/kml/2.2")]
    public class kml
    {
         public Document Document;
    }
    
  • 并序列化为

    XmlSerializerNamespaces ns  = new XmlSerializerNamespaces();
    ns.Add("","http://www.opengis.net/kml/2.2");
    new XmlSerializer(typeof(kml)).Serialize(stream, kml, ns);
    
于 2013-10-30T19:19:29.187 回答
0

您应该查看XML Schema Definition Tool (Xsd.exe),它将从KML Schema创建 C# 类。

于 2013-10-30T19:22:13.800 回答