2014-04-15 102 views
0

這裏有什麼問題?它引發ArgumentException無法從文件加載XML

var xmlDocument = await XmlDocument.LoadFromUriAsync(new Uri("ms-appx:///LiveTiles/Templates.xml")); 

是否有更簡單的方法從文件加載XML?

謝謝!

+0

你可以把異常的消息? –

+0

在此文章中回答了 [鏈接](http://stackoverflow.com/questions/9552335/how-to-open-a-packaged-file-with-winrt) – thsorens

回答

1

的Windows RT是在處理文件的時間有點特殊,我強烈推薦這個博客條目:

http://blog.jerrynixon.com/2012/06/windows-8-how-to-read-files-in-winrt.html

從鏈接摘自:

// settings 
// same as (ms-appx:///MyFolder/MyFile.txt) 
var _Folder = Windows.ApplicationModel.Package.Current.InstalledLocation; 
_Folder = await _Folder.GetFolderAsync("MyFolder"); 

// acquire file 
var _File = await _Folder.GetFileAsync("MyFile.txt"); 
Assert.IsNotNull(_File, "Acquire File"); 

// write content 
var _WriteThis = "Hello World"; 
await Windows.Storage.FileIO.WriteTextAsync(_File, _WriteThis); 

或者爲了簡化一點,另一種方法可以是:

嘗試

{ 

    StorageFolder storageFolder = Package.Current.InstalledLocation; 

    StorageFile storageFile = await storageFolder.GetFileAsync("BasicData.xml"); 



    XmlTextBlock.Text = await FileIO.ReadTextAsync(storageFile, Windows.Storage.Streams.UnicodeEncoding.Utf8); 



} 

catch (Exception ex) 

{ 

    XmlTextBlock.Text = ex.Message; 

}