用C#生成KML路径文件(下篇)

来源:互联网 发布:excel2013数据分析 编辑:程序博客网 时间:2024/04/29 07:33

因为KML实际是一种基于面向对象的标记语言,在上篇中我们对官方的KML路径文件进行分析,并提取中其中的对象进行抽象分析建立类。这样做的好处是程序具有很好的扩展性和可重用性。但是如果我们只需要生成具有固定格式的KML文件,可以采用一种更为简单的方法。


这里将用到System.Xml命名空间下的XmlTextWriter类,这个类允许将XML写到一个文件中。下面列出一些常用的方法:

WriteStartDocument 书写版本为“1.0”的XML声明

WriteEndDocument 关闭任何打开的元素或属性

WriteStartElement 开始一个元素

WriteEndElement 关闭一个元素

WriteElementString 编写具有制定本地名称和值的元素

WriteAttributeString 添加属性

WriteString 编写给定的文本内容

Close关闭流


依照惯例,还是将google官方的路径文件贴在这里,便于阅读。

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
  <Document>    <name>Paths</name>    <description>Examples of paths.</description>    <Style id="yellowLineGreenPoly">      <LineStyle>        <color>7f00ffff</color>        <width>4</width>      </LineStyle>      <PolyStyle>        <color>7f00ff00</color>      </PolyStyle>    </Style>    <Placemark>      <name>Absolute Extruded</name>      <description>Transparent green wall with yellow outlines</description>      <styleUrl>#yellowLineGreenPoly</styleUrl>      <LineString>        <extrude>1</extrude>        <tessellate>1</tessellate>        <altitudeMode>absolute</altitudeMode>        <coordinates> -112.2550785337791,36.07954952145647,2357          -112.2549277039738,36.08117083492122,2357                  </coordinates>      </LineString>    </Placemark>  </Document></kml>
接下来我们将上面的文档用XmlTextWriter一条一条写出来,如下:

<pre name="code" class="csharp">        static void test()        {            //生成KML文件,注意大小写            FileStream fs = new FileStream("path.xml", FileMode.Create);            XmlTextWriter w = new XmlTextWriter(fs, Encoding.UTF8);            // 开始文档            w.WriteStartDocument();            w.WriteStartElement("kml", "http://www.opengis.net/kml/2.2");            //开始一个元素            w.WriteStartElement("Document");            //添加子元素            w.WriteElementString("name", "Paths");               w.WriteElementString("description", "Examples of paths.");                     w.WriteStartElement("Style");            //向先前创建的元素中添加一个属性            w.WriteAttributeString("id", "yellowLineGreenPoly");                 w.WriteStartElement("LineStyle");            w.WriteElementString("color", "7f00ffff");            w.WriteElementString("width", "4");            w.WriteEndElement();            w.WriteStartElement("PolyStyle");            w.WriteElementString("color", "7f00ffff");            w.WriteEndElement();            // 关闭style元素            w.WriteEndElement();                 w.WriteStartElement("Placemark");            w.WriteElementString("name", "Absolute Extruded");            w.WriteElementString("description", "Transparent green wall with yellow outlines");            w.WriteElementString("styleUrl", "#yellowLineGreenPoly");                      w.WriteStartElement("LineString");            w.WriteElementString("extrude","1");            w.WriteElementString("tessellate","1");            w.WriteElementString("altitudeMode","absolute");                     w.WriteStartElement("coordinates");            // 将路径坐标写在这里                       w.WriteString("-112.2550785337791, 36.07954952145647, 2357 - 112.2549277039738, 36.08117083492122, 2357");            // 关闭所有元素            w.WriteEndDocument();            // 关闭流            w.Close();        }

注意我们可以将上面代码中的 w.WriteString("-112.2550785337791, 36.07954952145647, 2357 - 112.2549277039738, 36.08117083492122, 2357");这一句中的坐标值提取出来,改由外部传入一个类型为string参数,然后将我们要显示的坐标值写入该参数即可。

0 0