Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Windows Phone使用xml代码从url获取值_C#_Xml_Windows Phone_Rate - Fatal编程技术网

C# Windows Phone使用xml代码从url获取值

C# Windows Phone使用xml代码从url获取值,c#,xml,windows-phone,rate,C#,Xml,Windows Phone,Rate,我正在尝试从此url获取实时货币汇率: 这就是我所尝试的: 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&ToCur

我正在尝试从此url获取实时货币汇率:

这就是我所尝试的:

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#。如何在两个标记之间正确获取该值?

您可以尝试使用
WebClient
从internet下载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具有默认名称空间,因此它(即使
XDocument
创建成功,您当前的尝试也不会起作用)。

在~4小时后找到解决方案:


以下是我调用函数并转换结果的方式:

double rate = await getRate("GBP", "LTL");
string res = System.Convert.ToString(rate);
output.Text = res;
请注意,从中调用此函数的方法必须声明为异步函数本身,否则不能使用wait运算符


功能:

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);
        }

找不到类型或命名空间WebClient。我包括System.Net。也找不到DownloadStringCompletedEventArgs。什么版本的Windows Phone?WP7?WP8?WP8.1 Silverlight/Universal Apps?应该适用于所有人,因为有些人确认(除了WP8.1,可能有效,但尚未找到链接)不知道在WP 8.1中使用什么作为Webclient替代方案,以及如何处理下载的StringCompletedEventArgs。。
using System.Net;
using System.Xml;
using System.Xml.Linq;
using System.Net.Http;
using System.Threading.Tasks;