Windows 8 学习笔记(十四).map文件与.kml文件的解析


这段时间在做一个通过从网络上抓取的.map文件及区域图片,进行相应的加载定位,并将导航路径输出为.KML格式,以便下次加载显示上次路径。用过Google Earth的应该知道这两种文件格式。
.map文件解析
该文件不是XML文件格式,但却有固有的输出顺序,我只需按固定的顺序截取我要的信息即可,当然我这里有的最笨的方法,字符行的形式进行截取的,这个方法通用性太低,但我实在不知用哪种方式,若有知晓的,还忘告知~
复制代码
FileOpenPicker filepicker = new FileOpenPicker();
                filepicker.FileTypeFilter.Add(".map");
                filepicker.ViewMode = PickerViewMode.Thumbnail;
                StorageFile file = await filepicker.PickSingleFileAsync();
                if (null != file)
                {
                    IList<string> fileContent = await FileIO.ReadLinesAsync(file);
            。。。 
复制代码

}

.kml文件解析
kml文件是XML文件格式,但有细微的区别,它有头文件
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">

这样的格式C#中不能成功加载文件,我中间多走了一步去中转了下,将xmlns:kml格式先替换为正常的XML文件格式,等读取完成后再将其写回文件中去。 

.kml文件的读取 

 View Code

 
复制代码
FileOpenPicker filepicker = new FileOpenPicker();
                filepicker.FileTypeFilter.Add(".kml");
                filepicker.ViewMode = PickerViewMode.Thumbnail;
                StorageFile file = await filepicker.PickSingleFileAsync();
                //kml文件转义
                string fileContent = await FileIO.ReadTextAsync(file);
                string newstr = fileContent.Replace("xmlns:""renew");
                newstr = newstr.Replace("xmlns""topattr");
                await FileIO.WriteTextAsync(file, newstr);
                fileContent = await FileIO.ReadTextAsync(file);
        //按XML文件格式读取相应的节点

        。。。。
                
        //再将文件内容还原回去
        newstr = newstr.Replace("renew""xmlns:");
                newstr = newstr.Replace("topattr""xmlns");
复制代码

 几经周折,我的需求是满足了,不知道各位还有没有别的更好的方法呢?

Trackback:

http://www.cnblogs.com/jing870812/archive/2012/06/18/2553978.html

原文地址:https://www.cnblogs.com/hdjjun/p/2574969.html