Win/WP8.1 读取本地文件夹的xml文件

2014-08-16  籽藤 

读取APP本地文件夹中的xml文件。

下面的方式只适用于Win8 App,在Windows Phone上运行会报错:Cannot open 'file:///D:/WPSystem/AppData/Local/Packages/0d1585e5-1004-474a-9a2a-abd88487a42a_dhhx48j1ew2yc/LocalState/mylist.xml'. The Uri parameter must be a file system relative path.

public List<Phrase> ReadBingDictPhraseList(string filename) { _objects.Clear(); List<Phrase> phrases = XElement.Load(filename).Descendants("Phrase").Select( p => new Phrase() { Eng = p.Element("Eng").Value, Phonetic = p.Element("Phonetic").Value, Defi = p.Element("Defi").Value, Note = p.Element("Note").Value, Date = p.Element("Date").Value }).ToList(); foreach (var p in phrases) { _objects.Add(p); } return phrases; }

对于WinPhone,我们要采用StorageFile方式读取

public async Task<List<Phrase>> ReadBingDictPhraseList(StorageFile sf) { _objects.Clear(); var xmlfile = await sf.OpenAsync(FileAccessMode.Read); Stream stream = xmlfile.AsStreamForRead(); List<Phrase> phrases = XElement.Load(stream).Descendants("Phrase").Select( p => new Phrase() { Eng = p.Element("Eng").Value, Phonetic = p.Element("Phonetic").Value, Defi = p.Element("Defi").Value, Note = p.Element("Note").Value, Date = p.Element("Date").Value }).ToList(); foreach (var p in phrases) { _objects.Add(p); } return phrases; }

PS. Application.GetResourceStream不适用于Windows Phone 8.1,网上类似的用法都是Windows Phone 7

342°/3424 人阅读/0 条评论 发表评论

登录 后发表评论