如何打开与WinRT的一个打包文件 [英] How to open a packaged file with WinRT
问题描述
我试图弄清楚如何端口解析XML文件的WinRT一些的.Net代码。到目前为止,与的给定的System.Uri不能转换成Windows.Foundation.Uri ,我有下面的代码。不过,我立即得到卡住后,我创建开放的:
I am trying to figure out how to port some .Net code that parsed an xml file to WinRT. So far, with the help of The given System.Uri cannot be converted into a Windows.Foundation.Uri, I have the below code. Still, I get stuck immediately after I create the Uri:
static readonly Uri ResourcesBase = new Uri(@"ms-resource://MyAssembly/");
public override async void Load()
{
Uri uri = new Uri(ResourcesBase, filePath); // filePath = "Data//world.xml";
XmlLoadSettings settings = new XmlLoadSettings() { ValidateOnParse = false };
XmlDocument xmlDoc = await XmlDocument.LoadFromUriAsync(uri, settings);
foreach (IXmlNode xmlNode in xmlDoc.ChildNodes)
{
ProcessNode(xmlNode);
}
}
我得到一个未处理的异常,当我尝试调用的XmlDocument .LoadFromUriAsyn(URI):
I get an unhandled exception when I try to call XmlDocument.LoadFromUriAsyn(uri):
ArgumentException的是未处理的用户代码 - 值没有在预期范围内。
ArgumentException was unhandled by the user code - Value does not fall within the expected range.
任何人都觉得一切是WinRT的10倍更难
Anyone else feel like everything is 10 times harder with WinRT?
编辑:
我曾尝试以下所有字符串,并得到确切的同样的错误:
I have tried all the following strings, and get the exact same error:
Uri uri = new Uri("ms-resource://MyAssembly//" + filePath);
Uri uri = new Uri("ms-resource://MyAssembly/" + filePath);
Uri uri = new Uri("d:\\projects\\crystal\\" + filePath); // A valid absolute path
项目设置:
项目
- 属性
- 参考
- 资产
- 数据
- world.xml
- Properties
- References
- Assets
- Data
- world.xml
在代码:
filePath = "Data\\world.xml";
我也试图把XML文件下assset\data,只是资产。似乎没有什么有所作为。
I have also tried putting the xml file under assset\data, and just assets. Nothing seems to make a difference.
另外,我在XML设置为内容的生成操作。那是对的吗? 。唯一的其他东西我可以想像,这将是为嵌入的资源,但我怀疑它
Another thing, I have the Build Action of the xml set to "Content". Is that correct? The only other thing I could imagine that it would be is "Embedded Resource" but I doubt it.
全部异常详细信息:
System.ArgumentException是由用户代码
System.ArgumentException was unhandled by user code
HResult的= -2147024809
HResult=-2147024809
消息=值未处理。没有在预期范围之内。
Message=Value does not fall within the expected range.
来源= Windows.Data.Xml.Dom
Source=Windows.Data.Xml.Dom
堆栈跟踪:
在Windows.Data.Xml.Dom.XmlDocument.LoadFromUriAsync(URI URI,XmlLoadSettings loadSettings)
at Windows.Data.Xml.Dom.XmlDocument.LoadFromUriAsync(Uri uri, XmlLoadSettings loadSettings)
在水晶.IO.File.XmlFileSerializer.d__1.MoveNext()在d:\Projects\Crystal\library\IO\File\XmlFileSerializer.cs:第32行
at Crystal.IO.File.XmlFileSerializer.d__1.MoveNext() in d:\Projects\Crystal\library\IO\File\XmlFileSerializer.cs:line 32
的InnerException:
InnerException:
下载最小的例子可能瑞普问题: test_xml.zip
Download the smallest example possible to repro the issue: test_xml.zip
推荐答案
我终于想通了我看着的Windows运行XML数据API示例。
I finally figured it out after I looked at Windows Runtime Xml data API sample.
public override async Load() { var file = await GetPackagedFile("assets", "world.xml"); LoadXml(file); } private async void LoadXml(StorageFile file) { XmlLoadSettings settings = new XmlLoadSettings() { ValidateOnParse = false }; XmlDocument xmlDoc = await XmlDocument.LoadFromFileAsync(file, settings); foreach (IXmlNode xmlNode in xmlDoc.ChildNodes) { //ProcessNode(xmlNode); } } private async Task<StorageFile> GetPackagedFile(string folderName, string fileName) { StorageFolder installFolder = Windows.ApplicationModel.Package.Current.InstalledLocation; if (folderName != null) { StorageFolder subFolder = await installFolder.GetFolderAsync(folderName); return await subFolder.GetFileAsync(fileName); } else { return await installFolder.GetFileAsync(fileName); } } }
这篇关于如何打开与WinRT的一个打包文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!