C# 将WebClient替换为脱机XDocument

C# 将WebClient替换为脱机XDocument,c#,xml,visual-studio-2010,windows-phone-7,webclient,C#,Xml,Visual Studio 2010,Windows Phone 7,Webclient,我已经构建了一个完整的WindowsPhone7应用程序(我很自豪!),但我刚刚意识到,在我的应用程序中访问的XML文件被托管在我的网站上并没有真正的用途。因为它们从来都不需要更新,所以我决定简单地将它们作为项目的一部分包含进来更为合理。然而,我从中学习的大部分经验和教程都展示了如何在通过WebClient下载XML数据后将其解析到列表框中。那么,有没有一种简单的方法可以用离线XML加载程序替换WebClient 这基本上就是我的应用程序中有多少页面是被编码的,很明显,为了简单起见,我将一些特定

我已经构建了一个完整的WindowsPhone7应用程序(我很自豪!),但我刚刚意识到,在我的应用程序中访问的XML文件被托管在我的网站上并没有真正的用途。因为它们从来都不需要更新,所以我决定简单地将它们作为项目的一部分包含进来更为合理。然而,我从中学习的大部分经验和教程都展示了如何在通过WebClient下载XML数据后将其解析到列表框中。那么,有没有一种简单的方法可以用离线XML加载程序替换WebClient

这基本上就是我的应用程序中有多少页面是被编码的,很明显,为了简单起见,我将一些特定于我的应用程序的名字改成了关于人/名字/年龄的废话

namespace WindowsPhoneApplication14.Pages.Other
{
public partial class People : PhoneApplicationPage
{
    public People()
    {           
        InitializeComponent();

        Dispatcher.BeginInvoke((Action)(() => pplListBox.ItemsSource = ppldata));

        WebClient pplWebClient = new WebClient();

        pplWebClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(ppl_DownloadStringCompleted);
        pplWebClient.DownloadStringAsync(new Uri("http://www.mywebsite.com/ppl.xml"));
    }

    void ppl_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        if (e.Error != null)
            return;

        XElement xmlitem = XElement.Parse(e.Result);

        var ppldata = new List<PeopleClass>();

        foreach (XElement item in xmlitem.Elements("entry"))
        {
            var name = item.Element("name");
            var namevalue = (name == null) ? null : name.Value;
            var age = item.Element("age");
            var agevalue = (age == null) ? null : age.Value;

            ppldata.Add
                (new PeopleClass
                    {
                        Name = namevalue,
                        Age = agevalue,
                    }
                );
        }

        pplListBox.ItemsSource = ppldata;            

    }

    public class PeopleClass
    {
        public string Name { get; set; }
        public string Age { get; set; }
    }

    public System.Collections.IEnumerable ppldata { get; set; }

}

}
namespace WindowsPhoneApplication14.Pages.Other
{
公共部分类人员:PhoneApplicationPage
{
公众人士()
{           
初始化组件();
Dispatcher.BeginInvoke((操作)(()=>pplListBox.ItemsSource=ppldata));
WebClient pplWebClient=新的WebClient();
pplWebClient.DownloadStringCompleted+=新的DownloadStringCompletedEventHandler(ppl_DownloadStringCompleted);
pplWebClient.DownloadStringAsync(新Uri(“http://www.mywebsite.com/ppl.xml"));
}
void ppl_DownloadStringCompleted(对象发送方,DownloadStringCompletedEventArgs e)
{
如果(例如错误!=null)
返回;
XElement xmlitem=XElement.Parse(e.Result);
var ppldata=新列表();
foreach(xmlitem.Elements(“条目”)中的XElement项)
{
变量名称=项目元素(“名称”);
var namevalue=(name==null)?null:name.Value;
var年龄=项目要素(“年龄”);
var agevalue=(age==null)?null:age.Value;
ppldata.Add
(新人民阶级)
{
名称=名称值,
年龄=年龄值,
}
);
}
pplListBox.ItemsSource=ppldata;
}
公众阶级
{
公共字符串名称{get;set;}
公共字符串年龄{get;set;}
}
public System.Collections.IEnumerable ppldata{get;set;}
}
}

那么,我可以用什么替换WebClient操作呢?

您会注意到XDocument.Load有两组主要的覆盖。一个获取流(就像您在XElement.Parse(e.Result)中使用的那样),另一个获取XAP中XML文件的路径

如果文档是静态的,并且可以使用XAP发布,则可以使用后者

我发布的这个示例就是这样工作的


您会注意到XDocument.Load有两组主要的重写。一组接受流(就像您在XElement.Parse(e.Result)中使用的那样),另一组接受指向XAP中XML文件的路径

如果文档是静态的,并且可以使用XAP发布,则可以使用后者

我发布的这个示例就是这样工作的


再次感谢,米克。我会尽快尝试。再次感谢,米克。我会尽快尝试。