2010-08-23 81 views
1

我正在研究WP7应用程序。该应用程序将有几个XML文件。有些是可读写的,有些是只读的。我在这里有什么选择? IsolatedStorage?作为资源嵌入?如何在Windows Phone xap部署中包含XML数据?

我还需要知道...

  1. 我将如何加载XML到无论从XElements?
  2. 我将如何从XElement保存到任何一个?

回答

2

要进行写入访问,您需要使用IsolatedStorage。您可以检查文件是否存在,并从IsolatedStorage加载它,否则从资源加载它。对于只读访问,您可以从资源中加载它。将xml文件添加到项目中检查构建操作是内容。

XDocument doc = LoadFromIsolatedStorage(name); 
if (doc == null) 
{ 
    doc = LoadFromResource(name); 
} 

//////////////////////////// 

public static XDocument LoadFromResource(string name) 
{ 
    var streamInfo = Application.GetResourceStream(new Uri(name, UriKind.Relative)); 
    using(var s = streamInfo.Stream) 
     return XDocument.Load(s); 

} 

public static XDocument LoadFromIsolatedStorage(string name) 
{ 
    using (var store = IsolatedStorageFile.GetUserStoreForApplication()) 
    { 
     if (store.FileExists(name)) 
     { 
      using(var stream = store.OpenFile(name,FileMode.Open)) 
       return XDocument.Load(stream); 
     } 
     else 
      return null; 
    } 
} 

public static void SaveToIsolatedStorage(XDocument doc, string name) 
{ 
    using (var store = IsolatedStorageFile.GetUserStoreForApplication()) 
    { 
     var dir = System.IO.Path.GetDirectoryName(name); 
     if (!store.DirectoryExists(dir)) 
      store.CreateDirectory(dir); 
     using (var file = store.OpenFile(name, FileMode.OpenOrCreate)) 
      doc.Save(file); 
    } 
} 
+0

太棒了。谢谢。 – Shlomo 2010-08-24 02:56:44

0

想一想,你是否想将“xap”作为包含所有文档的“exe”运行?如果是这样,你可以使用工具Desklighter(http://blendables.com/labs/desklighter/)。

+0

这不起作用。它适用于Windows Phone。 – Shlomo 2010-08-25 00:28:39

相关问题