在 Windows Phone 7 (WP7) 中加载本地 xml 文件

发布于 2024-10-06 19:01:25 字数 907 浏览 3 评论 0原文

我正在尝试加载 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

水水月牙 2024-10-13 19:01:25

您还可以在 XML 文件的属性中将“Build Action”设置为“Content”,然后在后面的代码中:

using System.Xml.Linq;

......

XElement appDataXml;

StreamResourceInfo xml = Application.GetResourceStream(new Uri("yourxmlfilename.xml", UriKind.Relative)); 
appDataXml = XElement.Load(xml.Stream);

You can also set the "Build Action" to "Content" in the properties of your XML file, and then in the code behind:

using System.Xml.Linq;

......

XElement appDataXml;

StreamResourceInfo xml = Application.GetResourceStream(new Uri("yourxmlfilename.xml", UriKind.Relative)); 
appDataXml = XElement.Load(xml.Stream);
无悔心 2024-10-13 19:01:25

以下是加载 XML 文件的方法:

在 XML 文件的属性中将“构建操作”设置为“资源”,然后在代码中:

using System.Xml.Linq;

......

XElement appDataXml;

StreamResourceInfo xml = Application.GetResourceStream(new Uri("/yourprojectname;component/yourxmlfilename.xml", UriKind.Relative)); 
appDataXml = XElement.Load(xml.Stream);

Here is how to load an XML file:

Set the "Build action" to "Resource" in the properties of your XML file, then in code:

using System.Xml.Linq;

......

XElement appDataXml;

StreamResourceInfo xml = Application.GetResourceStream(new Uri("/yourprojectname;component/yourxmlfilename.xml", UriKind.Relative)); 
appDataXml = XElement.Load(xml.Stream);
涙—继续流 2024-10-13 19:01:25

这是我在 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文