這段時間在做一個通過從網絡上抓取的.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);
。。。
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 ");
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 ");
幾經周折,我的需求是滿足了,不知道各位還有沒有別的更好的方法呢?
