[英]How to read data from XML resource in Windows Store (Metro Style) app?
I have a windows store app. 我有一个Windows商店应用程序。 I have a XML file containing data.
我有一个包含数据的XML文件。 I need to add this file as resource of my app.
我需要将此文件添加为我的应用程序的资源。 I need to read data to XDocument from this file.
我需要从该文件读取数据到XDocument。
1) What build action I should set when add XML file to project? 1)将XML文件添加到项目时,我应该设置什么构建动作? (I think this is "Content")
(我认为这是“内容”)
2) How to get XDocument object from this XML file? 2)如何从此XML文件获取XDocument对象?
After 2 hours I've got this code: 2小时后,我得到了以下代码:
public static class DataLoader {
public static XDocument LoadFromXmlResource(string path){
path.Replace('\\', '/');
path.TrimEnd('/');
string uriPath = "ms-appx:///MyApp/" + path;
Task<StorageFile> operation = StorageFile.GetFileFromApplicationUriAsync(new Uri(uriPath)).AsTask<StorageFile>();
StorageFile file = operation.Result;
Task<string> fileReadingTask = FileIO.ReadTextAsync(file).AsTask<string>();
string fileContent = fileReadingTask.Result;
XDocument result = XDocument.Parse(fileContent, LoadOptions.None);
return result;
}
}
this works, but I'm not shure that it is correct. 这行得通,但我不确定它是正确的。
I have no foun another way. 我没有另一种方式。 You can try find another.
您可以尝试找到另一个。
This is the correct way to reference content in your application. 这是在应用程序中引用内容的正确方法。 You can also add a .resw file to your project to store resources for loading at runtime, via ResourceLoader.
您还可以将.resw文件添加到项目中,以存储资源,以通过ResourceLoader在运行时加载。
Resources differ since they are designed to be culture-sensitive, so they're good for translations of button text values etc. 资源不同,因为它们被设计为对文化敏感,因此它们对于按钮文本值的翻译等非常有用。
If you don't need this functionality, then assets added with the Build Action "Content" are perfect and you use the ms-appx or ms-appdata URI schemes to address them. 如果不需要此功能,则添加了带有“操作”“内容”的资产是完美的,您可以使用ms-appx或ms-appdata URI方案来解决这些问题。
Package content via ms-appx 通过ms-appx打包内容
http://msdn.microsoft.com/en-us/library/windows/apps/Hh781215.aspx http://msdn.microsoft.com/zh-CN/library/windows/apps/Hh781215.aspx
Mutable data via ms-appdata 通过ms-appdata的可变数据
http://msdn.microsoft.com/en-us/library/windows/apps/hh464917.aspx http://msdn.microsoft.com/zh-CN/library/windows/apps/hh464917.aspx
-and- -和-
http://msdn.microsoft.com/en-us/library/windows/apps/Hh781229.aspx http://msdn.microsoft.com/zh-CN/library/windows/apps/Hh781229.aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.