打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

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

因为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一条一条写出来,如下:

  1. <pre name="code" class="csharp"> static void test()
  2. {
  3. //生成KML文件,注意大小写
  4. FileStream fs = new FileStream("path.xml", FileMode.Create);
  5. XmlTextWriter w = new XmlTextWriter(fs, Encoding.UTF8);
  6. // 开始文档
  7. w.WriteStartDocument();
  8. w.WriteStartElement("kml", "http://www.opengis.net/kml/2.2");
  9. //开始一个元素
  10. w.WriteStartElement("Document");
  11. //添加子元素
  12. w.WriteElementString("name", "Paths");
  13. w.WriteElementString("description", "Examples of paths.");
  14. w.WriteStartElement("Style");
  15. //向先前创建的元素中添加一个属性
  16. w.WriteAttributeString("id", "yellowLineGreenPoly");
  17. w.WriteStartElement("LineStyle");
  18. w.WriteElementString("color", "7f00ffff");
  19. w.WriteElementString("width", "4");
  20. w.WriteEndElement();
  21. w.WriteStartElement("PolyStyle");
  22. w.WriteElementString("color", "7f00ffff");
  23. w.WriteEndElement();
  24. // 关闭style元素
  25. w.WriteEndElement();
  26. w.WriteStartElement("Placemark");
  27. w.WriteElementString("name", "Absolute Extruded");
  28. w.WriteElementString("description", "Transparent green wall with yellow outlines");
  29. w.WriteElementString("styleUrl", "#yellowLineGreenPoly");
  30. w.WriteStartElement("LineString");
  31. w.WriteElementString("extrude","1");
  32. w.WriteElementString("tessellate","1");
  33. w.WriteElementString("altitudeMode","absolute");
  34. w.WriteStartElement("coordinates");
  35. // 将路径坐标写在这里
  36. w.WriteString("-112.2550785337791, 36.07954952145647, 2357 - 112.2549277039738, 36.08117083492122, 2357");
  37. // 关闭所有元素
  38. w.WriteEndDocument();
  39. // 关闭流
  40. w.Close();
  41. }

注意我们可以将上面代码中的 w.WriteString("-112.2550785337791, 36.07954952145647, 2357 - 112.2549277039738, 36.08117083492122, 2357");这一句中的坐标值提取出来,改由外部传入一个类型为string参数,然后将我们要显示的坐标值写入该参数即可。
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
XmlTextWriter类
写XML
基于.NET技术的RSS订阅开发实例
C#操作xml
C#在WINForm程序中创建XML文件
还有什么不能做?
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服