用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"?>接下来我们将上面的文档用XmlTextWriter一条一条写出来,如下:<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>
<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
- 用C#生成KML路径文件(下篇)
- 用C#生成KML路径文件(上篇)
- c#生成KML文件
- Java生成kml文件
- 用ARCGIS能不能把坐标数据生成KML文件
- Arcgis 栅格图像生成KML文件
- 使用一个pcap包生成kml文件
- Java生成KML文件,用OpenLayers技术显示在地图上.
- ENVI栅格裸数据生成shp和kml矢量文件
- KML文件基本格式
- kml文件初探
- GOOGLE地球浏览器分析(三):KML文件的定义
- GOOGLE地球浏览器分析(三):KML文件的定义
- GOOGLE地球浏览器分析(五):KML文件基本格式
- GOOGLE地球浏览器分析(五):KML文件基本格式
- GOOGLE地球浏览器分析(五):KML文件基本格式
- GOOGLE地球浏览器分析(三):KML文件的定义
- 第三部分:在DVR中生成KML格式GPS的轨迹文件
- hdoj 2586 How far away ? 【LCA转RMQ入门题】
- PyDev for Eclipse 简介
- unity学习正式开始
- 撒旦法阿斯
- HDOJ1317 XYZZY(floyd + bellman)
- 用C#生成KML路径文件(下篇)
- Spring的基本用法
- 单例模式
- UIViewController——Handling View-Related Notifications(处理与视图相关的通知)
- 撒旦法
- linux 线程id 与进程id对应关系
- 并查集详解
- 阿斯蒂芬撒的萨芬
- NYOJ 49 开心的小明