【问题标题】:Windows Phone get value from url with xml codeWindows Phone 使用 xml 代码从 url 获取值
【发布时间】:2014-07-26 12:49:51
【问题描述】:

我正在尝试从此网址获取实时汇率: http://www.webservicex.net/CurrencyConvertor.asmx/ConversionRate?FromCurrency=GBP&ToCurrency=LTL

这是我尝试过的:

public void getRate(string howmuch, string from, string to)
    {
        int hmuch = int.Parse(howmuch);
        string url = "http://www.webservicex.net/CurrencyConvertor.asmx/ConversionRate?FromCurrency=GBP&ToCurrency=LTL";
        var xml = XDocument.Load(url);
        var result = xml.Descendants("double");
        btn.Content = result;
    }

我从 XDocument.Load 收到一个错误,我需要从文件系统传递 URI,而不是从 Web 传递 URL。我没有在网上找到正确的方法来在 windows phone 中执行此操作,只有完整的 C#。如何在双标签之间正确获取该值?

【问题讨论】:

    标签: c# xml windows-phone rate


    【解决方案1】:

    您可以尝试使用WebClient 从互联网下载 XML 内容:

    WebClient wc = new WebClient();
    wc.DownloadStringCompleted += DownloadCompleted;
    wc.DownloadStringAsync(new Uri(" http://www.webservicex.net/CurrencyConvertor.asmx/ConversionRate?FromCurrency=GBP&ToCurrency=LTL"));
    

    然后使用XDocument.Parse()将其加载到XDocument对象:

    private void DownloadCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        if (e.Error == null)
        {
            XDocument xdoc = XDocument.Parse(e.Result);
            var result = xml.Root;
            btn.Content = result;
        }
    }
    

    请注意,您的 XML 具有默认命名空间,因此它是 should be handled a bit differently(即使 XDocument 已成功创建,您当前的尝试也不会起作用)。

    【讨论】:

    • 类型或命名空间 WebClient 找不到。我包括 System.Net。也找不到 DownloadStringCompletedEventArgs。
    • 什么版本的 Windows Phone? WP7? WP8? WP8.1 Silverlight/通用应用程序?
    • 应该对所有人都有效,因为有些人证实了这一点(WP7WP8,除了 WP8.1,可能有效但尚未找到链接)
    • 不知道在 WP 8.1 中使用什么作为 Webclient 替代方案以及如何处理 DownloadStringCompletedEventArgs..
    【解决方案2】:

    在约 4 小时后找到解决方案


    这就是我调用函数和转换结果的方式:

    double rate = await getRate("GBP", "LTL");
    string res = System.Convert.ToString(rate);
    output.Text = res;
    

    请注意,调用此函数的方法必须声明为 async 作为函数本身,否则不能使用 await 运算符。


    功能:

    public async Task<double> getRate(string from, string to)
            {
                string xml = string.Empty;
                Uri url = new Uri("http://www.webservicex.net/CurrencyConvertor.asmx/ConversionRate?FromCurrency="+from+"&ToCurrency="+to);
                HttpClient httpClient = new HttpClient();
                httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Accept", "text/html,application/xhtml+xml,application/xml");
                var response = await httpClient.GetAsync(url);
                using (var responseStream = await response.Content.ReadAsStreamAsync())
                using (var streamReader = new StreamReader(responseStream))
                {
                   xml = streamReader.ReadToEnd();
                }
                XDocument xDoc = XDocument.Parse(xml);
                XNamespace xmlns = "http://www.webserviceX.NET/";
                string value = (string)xDoc.Element(xmlns + "double");
                return System.Convert.ToDouble(value);
            }
    

    也不要忘记包含所需的标题

    using System.Net;
    using System.Xml;
    using System.Xml.Linq;
    using System.Net.Http;
    using System.Threading.Tasks;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-08
      • 1970-01-01
      • 2015-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多