我正在尝试将一些解析xml文件的.Net代码移植到WinRT平台。到目前为止,在获得给定的 System.Uri 无法转换为 Windows.Foundation.Uri 的帮助后,我有了以下代码。但是,在创建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)时,出现未处理的异常:
参数异常未被用户代码处理 - 值不在预期范围内。
有没有其他人感觉使用WinRT时所有事情都变得十倍困难?
编辑:
我已经尝试了以下所有字符串,并且得到完全相同的错误:
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
- 源代码...
在代码中:
filePath = "Data\\world.xml";
我还尝试过将xml文件放置在asset\data和仅asset目录下,但似乎没有任何改变。另外,我将xml的生成操作设置为“Content”。这样做正确吗?我认为唯一可能的选项是“Embedded Resource”,但我不确定。完整的异常细节:
System.ArgumentException was unhandled by user code HResult=-2147024809
Message=Value does not fall within the expected range.
Source=Windows.Data.Xml.Dom
StackTrace:
at Windows.Data.Xml.Dom.XmlDocument.LoadFromUriAsync(Uri uri, XmlLoadSettings loadSettings)
at Crystal.IO.File.XmlFileSerializer.d__1.MoveNext() in d:\Projects\Crystal\library\IO\File\XmlFileSerializer.cs:line 32
InnerException:
下载最小化的可重现问题的示例:test_xml.zip
Uri uri = new Uri("ms-resource://MyAssembly/" + filePath);
。 - Damir ArhfilePath
是什么样子吗? - user7116