如何使用C#Windows Phone 8.1从URL读取XML数据

时间:2015-07-06 11:24:30

标签: c# windows-phone-8.1

我在我的Windows 8.1项目上编写了这个代码块。但是我的Windows Phone 8.1项目没有工作

  private void Page_Loaded(object sender, RoutedEventArgs e)
    {

        Uri url = new Uri("http://www.tcmb.gov.tr/kurlar/today.xml");
        XDocument xml = XDocument.Load(url.ToString());
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.LoadXml(xml.ToString());
    }

1 个答案:

答案 0 :(得分:2)

首先,我下载了XML,Windows Phone不支持“ISO-8859-9”。

其次,为了使用XDocument,您需要下载该文件并将该流作为参数发送到Load方法。

以下是一个例子:

public void LoadXML()
{
    HttpClient client = new HttpClient();
    var httpResponseMessage = await client.GetAsync(new Uri("http://thewindev.net/post-sitemap.xml"));
    if (httpResponseMessage.StatusCode == HttpStatusCode.OK)
    {
        var xmlStream = await httpResponseMessage.Content.ReadAsStreamAsync();
        XDocument xml = XDocument.Load(xmlStream);
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.LoadXml(xml.ToString());
    }
}