所以我知道当我测试在文本块中显示数据时正在读取文件,但似乎无法将 xml 分解为我想要的部分,即货币名称和汇率,然后将它们添加到列表中以供选择和之后进行货币计算。
using System.Xml.Linq;
namespace Mike_sMoney
{
public partial class MainPage : PhoneApplicationPage
{
//String curName;
//double curRate;
WebClient myClient;
//public class Rate
//{
// public String curName { get; set; }
// public double curRate { get; set; }
//}
// Constructor
public MainPage()
{
InitializeComponent();
myClient = new WebClient();
myClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(myClient_DownloadStringCompleted);
string urlRate = "http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml";
myClient.DownloadStringAsync(new Uri(urlRate));
//XDocument rateDoc = XDocument.Load(new StringReader(urlRate));
//var rates = from rate in rateDoc.Descendants("Cube")
// select new Rate
// {
// curName = rate.Attribute("currency").Value,
// curRate = double.Parse(rate.Attribute("rate").Value)
// };
}
void myClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error == null)
{
//textBlock1.Text = e.Result;
XElement currencyElements = XElement.Parse(e.Result);
var curList = from mRate in currencyElements.Descendants("Cube")
select new ClassRates
{
currency=mRate.Element("currency").Value,
rate=Convert.ToDouble(mRate.Element("rate").Value)
};
fromListBox.ItemsSource = curList;
}
else
{
textBlock1.Text = e.Error.ToString();
}
}
private void convertButton_Click(object sender, RoutedEventArgs e)
{
double x;
//string urlRate = "http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml";
//myClient.DownloadStringAsync(new Uri(urlRate));
//x = urlRate.getElementsByTagName("rate")[0];
}
private void amountTextBox_TextChanged(object sender, TextChangedEventArgs e)
{
amountTextBox.SelectAll();
}
}
}
<gesmes:Envelope xmlns:gesmes="http://www.gesmes.org/xml/2002-08-01" xmlns="http://www.ecb.int/vocabulary/2002-08-01/eurofxref">
<gesmes:subject>Reference rates</gesmes:subject>
<gesmes:Sender>
<gesmes:name>European Central Bank</gesmes:name>
</gesmes:Sender>
<Cube>
<Cube time="2012-04-05">
<Cube currency="USD" rate="1.3068"/>
<Cube currency="JPY" rate="107.06"/>
<Cube currency="BGN" rate="1.9558"/>
<Cube currency="CZK" rate="24.704"/>
<Cube currency="DKK" rate="7.4397"/>
<Cube currency="GBP" rate="0.82420"/>
<Cube currency="HUF" rate="295.95"/>
<Cube currency="LTL" rate="3.4528"/>
<Cube currency="LVL" rate="0.6995"/>
<Cube currency="PLN" rate="4.1707"/>
<Cube currency="RON" rate="4.3728"/>
<Cube currency="SEK" rate="8.8134"/>
<Cube currency="CHF" rate="1.2025"/>
<Cube currency="NOK" rate="7.5692"/>
<Cube currency="HRK" rate="7.4820"/>
<Cube currency="RUB" rate="38.6600"/>
<Cube currency="TRY" rate="2.3468"/>
<Cube currency="AUD" rate="1.2710"/>
<Cube currency="BRL" rate="2.3942"/>
<Cube currency="CAD" rate="1.3042"/>
<Cube currency="CNY" rate="8.2398"/>
<Cube currency="HKD" rate="10.1478"/>
<Cube currency="IDR" rate="11945.92"/>
<Cube currency="ILS" rate="4.8967"/>
<Cube currency="INR" rate="66.8750"/>
<Cube currency="KRW" rate="1479.25"/>
<Cube currency="MXN" rate="16.8244"/>
<Cube currency="MYR" rate="4.0106"/>
<Cube currency="NZD" rate="1.6026"/>
<Cube currency="PHP" rate="55.897"/>
<Cube currency="SGD" rate="1.6476"/>
<Cube currency="THB" rate="40.511"/>
<Cube currency="ZAR" rate="10.2687"/>
</Cube>
</Cube>
</gesmes:Envelope>
非常感谢任何想法或帮助 mh