在 Windows Phone 7 (WP7) 中加载本地 xml 文件
我正在尝试加载 Windows Phone 7 解决方案中的 XML 文件。我想知道将文件包含在项目中然后用代码引用它的正确方法。我当前的代码给我一个
NotSupportedException 错误 “WebClient 请求期间发生异常。”
这是我的 WebClient 代码的存根
WebClient workbenchAdConfigRequest = new WebClient();
workbenchAdConfigRequest.OpenReadCompleted += new OpenReadCompletedEventHandler(workbenchAdConfigRequest_OpenReadCompleted);
workbenchAdConfigRequest.OpenReadAsync(new Uri("/SampleData/SampleData.xml", UriKind.Relative));
,事件处理程序
private void workbenchAdConfigRequest_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
try
{
XElement xml = XElement.Load(e.Result);
}
catch
{
//return
}
}
位于文件 /SampleData/SampleData.xml 我已将属性设置为
“构建操作:嵌入式参考” 复制到输出目录:不复制 自定义工具:MSBuild:Compile
您可以从应用程序的“文件路径”加载本地文件吗?
I am trying to load up an XML file that lives in my Windows Phone 7 solution. I'd like to know the proper way to include the file in the project and then reference it with code. My current code is giving me an error of
NotSupportedException
"An exception occurred during a WebClient request."
here is the stub of my WebClient code
WebClient workbenchAdConfigRequest = new WebClient();
workbenchAdConfigRequest.OpenReadCompleted += new OpenReadCompletedEventHandler(workbenchAdConfigRequest_OpenReadCompleted);
workbenchAdConfigRequest.OpenReadAsync(new Uri("/SampleData/SampleData.xml", UriKind.Relative));
and the event handler is
private void workbenchAdConfigRequest_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
try
{
XElement xml = XElement.Load(e.Result);
}
catch
{
//return
}
}
on the file /SampleData/SampleData.xml I have set the properties to be
Build Action: Embedded Reference
Copy to Output Directory: Do Not Copy
Custom Tool: MSBuild:Compile
Can you load a local file from the "file path" of the application?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您还可以在 XML 文件的属性中将“Build Action”设置为“Content”,然后在后面的代码中:
......
You can also set the "Build Action" to "Content" in the properties of your XML file, and then in the code behind:
......
以下是加载 XML 文件的方法:
在 XML 文件的属性中将“构建操作”设置为“资源”,然后在代码中:
......
Here is how to load an XML file:
Set the "Build action" to "Resource" in the properties of your XML file, then in code:
......
这是我在 MSDN 上发布的一个工作项目,该项目将 XML 存储在 XAP 中并使用 XDocument/LINQ 加载它,将结果绑定到 ListBox。
将 Linq 数据源绑定到列表框
Here's a working project I posted on MSDN doing just that that stored XML in the XAP and loads it with XDocument/LINQ, binding the result to a ListBox.
binding a Linq datasource to a listbox