我想在C#中创建一个kml文件。现在我有两个问题:


为了在kml文件中包含以下内容,在XML文件中添加kml元素的synatx是什么?

<kml xmlns="http://www.opengis.net/kml/2.2">

我有一些要组成线串的点。我应该如何在kml文件的xml中填充坐标元素?以下是到目前为止的代码。


码:

public void MakeKmlFile(string line)
{
    CoordinateCollection coordinates = new CoordinateCollection();

    char[] delimiterLine = { '|' };
    char[] delimiterPoint = { ',' };
    string[] route = line.Split(delimiterLine);

    foreach (string point in route)
    {
        string[] route_point = line.Split(delimiterPoint);
        double lat = double.Parse(route_point[1]);
        double lon = double.Parse(route_point[0]);
        coordinates.Add(new Vector(lat, lon));
    }

    XmlTextWriter writer = new XmlTextWriter("route.xml", System.Text.Encoding.UTF8);
    writer.Formatting = Formatting.Indented;
    writer.WriteStartElement("Document");
    writer.WriteStartElement("Folder");
    writer.WriteStartElement("name");
    writer.WriteString("route");
    writer.WriteEndElement();
    writer.WriteStartElement("Placemark");
    writer.WriteStartElement("Style");
    writer.WriteStartElement("LineStyle");
    writer.WriteStartElement("color");
    writer.WriteString("ff0000ff");
    writer.WriteEndElement();
    writer.WriteEndElement();
    writer.WriteStartElement("PolyStyle");
    writer.WriteStartElement("fill");
    writer.WriteString("2");
    writer.WriteEndElement();
    writer.WriteEndElement();
    writer.WriteEndElement();
    writer.WriteStartElement("LineString");
    writer.WriteStartElement("coordinates");


这是我得到的结果:

<?xml version="1.0" encoding="utf-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
  <Document>
    <Name>Points.kml</Name>
    <Placemark />
    <Placemark />
    <Placemark />
    <Placemark />
    <Placemark />
  </Document>

最佳答案

您可以像创建普通XML文档一样创建KML文档

    XmlDocument xDoc = new XmlDocument();
    XmlDeclaration xDec = xDoc.CreateXmlDeclaration("1.0", "utf-8", null);

    XmlElement rootNode = xDoc.CreateElement("kml");
    rootNode.SetAttribute("xmlns", @"http://www.opengis.net/kml/2.2");
    xDoc.InsertBefore(xDec, xDoc.DocumentElement);
    xDoc.AppendChild(rootNode);
    XmlElement docNode = xDoc.CreateElement("Document");
    rootNode.AppendChild(docNode);

    XmlElement nameNodeMain = xDoc.CreateElement("Name");
    XmlText nameTextMain = xDoc.CreateTextNode("Points.kml");
    docNode.AppendChild(nameNodeMain);
    nameNodeMain.AppendChild(nameTextMain);


这样就为您的文档设置了基本结构,然后您要做的就是添加每个地标(最好通过循环完成)

    char[] delimiterLine = { '|' };
    char[] delimiterPoint = { ',' };
    string[] places = line.Split(delimiterLine);
    for (int i = 0; i < places.length; i++)
        {
            string[] placeMark = places[i].split(delimiterPoint);
            XmlElement placeNode = xDoc.CreateElement("Placemark");
            docNode.AppendChild(placeNode);

            XmlElement nameNode = xDoc.CreateElement("Name");
            XmlText nameText = xDoc.CreateTextNode(placeMark[0]);
            placeNode.AppendChild(nameNode);
            nameNode.AppendChild(nameText);

            XmlElement descNode = xDoc.CreateElement("Description");
            XmlText descText = xDoc.CreateTextNode(placeMark[1]);
            placeNode.AppendChild(descNode);
            descNode.AppendChild(descText);

            XmlElement pointNode = xDoc.CreateElement("Point");
            placeNode.AppendChild(pointNode);

            XmlElement coordNode = xDoc.CreateElement("coordinates");
            XmlText coordText = xDoc.CreateTextNode(string.Format("{0},{1}", placeMark[2], placeMark[3]));
            pointNode.AppendChild(coordNode);
            coordNode.AppendChild(coordText);
        }
        return xDoc;


我以前没有在KML中使用LineStrings,但是我怀疑执行此操作的代码将遵循以下内容:

    XmlDocument xDoc = new XmlDocument();
    XmlDeclaration xDec = xDoc.CreateXmlDeclaration("1.0", "utf-8", null);

    XmlElement rootNode = xDoc.CreateElement("kml");
    rootNode.SetAttribute("xmlns", @"http://www.opengis.net/kml/2.2");
    xDoc.InsertBefore(xDec, xDoc.DocumentElement);
    xDoc.AppendChild(rootNode);
    XmlElement docNode = xDoc.CreateElement("Document");
    rootNode.AppendChild(docNode);

    XmlElement nameNodeMain = xDoc.CreateElement("Name");
    XmlText nameTextMain = xDoc.CreateTextNode("Points.kml");
    docNode.AppendChild(nameNodeMain);
    nameNodeMain.AppendChild(nameTextMain);

XmlElement placeNode = xDoc.CreateElement("Placemark");
docNode.AppendChild(placeNode);

XmlElement nameNode = xDoc.CreateElement("Name");
XmlText nameText = xDoc.CreateTextNode("Test line");
placeNode.AppendChild(nameNode);
nameNode.AppendChild(nameText);

XmlElement lineStringNode = xDoc.CreateElement("LineString");
placeNode.AppendChild(lineStringNode);

XmlElement coordNode = xDoc.CreateElement("coordinates");

char[] delimiterLine = { '|' };
    char[] delimiterPoint = { ',' };
    string[] places = line.Split(delimiterLine);
    for (int i = 0; i < places.length; i++)
    {
    string[] placeMark = places[i].split(delimiterPoint);

    XmlText coordText = xDoc.CreateTextNode(string.Format("{0},{1}", placeMark[0], placeMark[1]));
    pointNode.AppendChild(coordNode);
   }


coordNode.AppendChild(coordText);

xDoc.Save("./KML/");


基本上,这涉及到移动我之前的代码,并为KML文件中所需的每个主要元素创建一个XmlElement,然后在将其分割为行字符串之后迭代坐标。

10-08 00:53