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