I'm trying to build a very simple C# app to download the latest RUB-USD exchange rate. www.cbr.ru provides a web service to pull the information from. I am first calling the function to get the latest exchange rate date. Then using that datetime to make the call to get the exchange rate on a particular date.
I'm able to pull the exchange rates XML and display it through StringReader. I used Pasted XML as Classes to create a class using this XML. When doing the coding, it seems to be working, as my rates variable autosuggests the expected attributes, i.e. rates.Vname, rates.Vcode, etc... however the program crashes (generic Windows error message - "NewExchangeRateService has stopped working") when it gets to Deserialization step.
Here's what I've got, some lines are simply part of my process and can obviously be ignored...
public Form1()
{
InitializeComponent();
CBR.ru.DailyInfoSoapClient rublesClient = new DailyInfoSoapClient();
DateTime lastRUB = rublesClient.GetLatestDateTime();
MessageBox.Show(lastRUB.ToShortDateString());
var RubRateXml = rublesClient.GetCursOnDateXML(lastRUB);
DataSet RUBrate = rublesClient.GetCursOnDate(lastRUB);
StringReader sr = new StringReader(RUBrate.GetXml());
richTextBox1.Text = sr.ReadToEnd();
XmlSerializer xs = new XmlSerializer(typeof (ValuteDataValuteCursOnDate));
var rates = (ValuteDataValuteCursOnDate) xs.Deserialize(sr);
MessageBox.Show(rates.Vname);
}
And the XML looks like:
<ValuteData>
<ValuteCursOnDate>
<Vname>Доллар США</Vname>
<Vnom>1</Vnom>
<Vcurs>53.1088</Vcurs>
<Vcode>840</Vcode>
<VchCode>USD</VchCode>
</ValuteCursOnDate>
</ValuteData>
This generates the following classes:
public partial class ValuteData
{
private ValuteDataValuteCursOnDate[] valuteCursOnDateField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("ValuteCursOnDate")]
public ValuteDataValuteCursOnDate[] ValuteCursOnDate
{
get { return this.valuteCursOnDateField; }
set { this.valuteCursOnDateField = value; }
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class ValuteDataValuteCursOnDate
{
private string vnameField;
private ushort vnomField;
private decimal vcursField;
private ushort vcodeField;
private string vchCodeField;
/// <remarks/>
public string Vname
{
get { return this.vnameField; }
set { this.vnameField = value; }
}
/// <remarks/>
public ushort Vnom
{
get { return this.vnomField; }
set { this.vnomField = value; }
}
/// <remarks/>
public decimal Vcurs
{
get { return this.vcursField; }
set { this.vcursField = value; }
}
/// <remarks/>
public ushort Vcode
{
get { return this.vcodeField; }
set { this.vcodeField = value; }
}
/// <remarks/>
public string VchCode
{
get { return this.vchCodeField; }
set { this.vchCodeField = value; }
}
}
I'm sure this is extremely simple and I just have a misunderstanding somewhere on the use of the deserialization, so hopefully somebody can help me quickly. If you'd like to take it one step further, I next will need to figure out how to pull the Vcurs where VchCode = USD (this is just a sample of the XML, there are lots of other rates included also)
Thanks!
No comments:
Post a Comment