将 XML 文件从网站加载到 XDocument(Silverlight 和 Windows Phone 7)

发布于 2024-09-28 14:11:03 字数 260 浏览 5 评论 0原文

我有一个 XML 文件,想要在 Windows Phone 7 和 Silverlight 应用程序中访问该文件。 XML 文件位于网络服务器上,我想通过 http://www.mydomain 访问它。 com/data/this_is_my_file.xml

如何使用此 URL 将 XML 文件加载到 XDocument 中?

I have an XML file that I want to access in an Windows Phone 7 and Silverlight application.
Th XML file is on a webserver, and I want to access it through http://www.mydomain.com/data/this_is_my_file.xml.

How do I use this URL to load the XML file into an XDocument?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

懒的傷心 2024-10-05 14:11:03

您可以使用 WebClientHttpWebRequest 下载(异步)并解析响应。从 Web 下载和解析 XML 的最简单方法之一如下 -

public void LoadXmlItems(string xmlUrl)
{
   WebClient client = new WebClient();
   
   client.OpenReadCompleted += (sender, e) =>
   {
        if (e.Error != null)
            return;

        Stream str = e.Result;
        XDocument xdoc = XDocument.Load(str);

        // take 10 first results
        List<RssFeedItem> rssFeedItems = (from item in xdoc.Descendants("item")
                                            select new XmlItem()
                                            {
                                                Title = item.Element("title").Value,
                                                Description = item.Element("description").Value,
                                                Url = new Uri(item.Element("link").Value, UriKind.Absolute)
                                            }).ToList();
        // close
        str.Close();

        // add results to the list
        XmlItems.Clear();
        foreach (RssFeedItem item in rssFeedItems)
        {
           XmlItems.Add(item);
        }
    };
    client.OpenReadAsync(new Uri(xmlUrl, UriKind.Absolute));
}

xmlUrl 是 Web 上 XML 文件的路径。 XmlItem 是一个像这样的类 -

public class XmlItem
{
  public string Title { get; set; }
  public string Description { get; set; }
  public Uri Url { get; set; }
}

您需要注意,如果您正在更新可观察集合,则可能会遇到跨线程异常。在上面的示例中,XmlItems 是一个 List。但是,如果您希望将 XMLItem 添加到可观察集合中,请使用这段代码 -

Dispatcher.BeginInvoke(() =>
{
  XmlItems.Clear();
  foreach (RssFeedItem item in rssFeedItems)
  {
     XmlItems.Add(item);
  }
});

另一种方法是使用 HttpWebRequest。您可以 在此处了解此方法 并使用示例中的代码。

HTH,indyfromoz

You can use WebClient or HttpWebRequest to download (asynchronously) and parse the response. One of the simplest approach to download and parse XML from the web is below -

public void LoadXmlItems(string xmlUrl)
{
   WebClient client = new WebClient();
   
   client.OpenReadCompleted += (sender, e) =>
   {
        if (e.Error != null)
            return;

        Stream str = e.Result;
        XDocument xdoc = XDocument.Load(str);

        // take 10 first results
        List<RssFeedItem> rssFeedItems = (from item in xdoc.Descendants("item")
                                            select new XmlItem()
                                            {
                                                Title = item.Element("title").Value,
                                                Description = item.Element("description").Value,
                                                Url = new Uri(item.Element("link").Value, UriKind.Absolute)
                                            }).ToList();
        // close
        str.Close();

        // add results to the list
        XmlItems.Clear();
        foreach (RssFeedItem item in rssFeedItems)
        {
           XmlItems.Add(item);
        }
    };
    client.OpenReadAsync(new Uri(xmlUrl, UriKind.Absolute));
}

xmlUrl is the path to the XML file on the web. XmlItem is a class like so -

public class XmlItem
{
  public string Title { get; set; }
  public string Description { get; set; }
  public Uri Url { get; set; }
}

You need to note that you may encounter cross-thread exception if you are updating an observable collection. In the above example, XmlItems is a List<XmlItem>. However, if you wish to add the XMLItem's to an observable collection, use this piece of code instead -

Dispatcher.BeginInvoke(() =>
{
  XmlItems.Clear();
  foreach (RssFeedItem item in rssFeedItems)
  {
     XmlItems.Add(item);
  }
});

An alternative approach is to use HttpWebRequest. You can read about this approach here and use the code in the sample.

HTH, indyfromoz

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