c# - XML Deserialization from a StringReader Crashes -


i'm trying build simple c# app download latest rub-usd exchange rate. www.cbr.ru provides web service pull information from. first calling function latest exchange rate date. using datetime make call exchange rate on particular date.

i'm able pull exchange rates xml , display through stringreader. used pasted xml classes create class using xml. when doing coding, seems working, rates variable autosuggests expected attributes, i.e. rates.vname, rates.vcode, etc... program crashes (generic windows error message - "newexchangerateservice has stopped working") when gets deserialization step.

here's i've got, lines part of process , can 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 xml looks like:

<valutedata>   <valutecursondate>     <vname>Доллар США</vname>                                                                                                                                                                                                                                                   <vnom>1</vnom>     <vcurs>53.1088</vcurs>     <vcode>840</vcode>     <vchcode>usd</vchcode>   </valutecursondate> </valutedata> 

this generates following classes:

[system.xml.serialization.xmltypeattribute(anonymoustype = true)] [system.xml.serialization.xmlrootattribute(namespace = "", isnullable = false)] public partial class valutedata {      private valutedatavalutecursondate[] valutecursondatefield;      /// <remarks/>     [system.xml.serialization.xmlelementattribute("valutecursondate")]     public valutedatavalutecursondate[] valutecursondate     {         { 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     {         { return this.vnamefield; }         set { this.vnamefield = value; }     }      /// <remarks/>     public ushort vnom     {         { return this.vnomfield; }         set { this.vnomfield = value; }     }      /// <remarks/>     public decimal vcurs     {         { return this.vcursfield; }         set { this.vcursfield = value; }     }      /// <remarks/>     public ushort vcode     {         { return this.vcodefield; }         set { this.vcodefield = value; }     }      /// <remarks/>     public string vchcode     {         { return this.vchcodefield; }         set { this.vchcodefield = value; }     } } 

i'm sure extremely simple , have misunderstanding somewhere on use of deserialization, can me quickly. if you'd take 1 step further, next need figure out how pull vcurs vchcode = usd (this sample of xml, there lots of other rates included also)

you call readtoend, , try deserialize same stringreader. have read entire reader.

also, should try this:

public form1() {     initializecomponent(); }  public void loaddata() {      cbr.ru.dailyinfosoapclient rublesclient = new dailyinfosoapclient();     datetime lastrub = rublesclient.getlatestdatetime();     // messagebox.show(lastrub.toshortdatestring());     var rubratexml = rublesclient.getcursondatexml(lastrub);     dataset rubrate = rublesclient.getcursondate(lastrub);     string xml = rubrate.getxml();     valutedatavalutecursondate rates;     using (stringreader sr = new stringreader(xml))     {         xmlserializer xs = new xmlserializer(typeof (valutedatavalutecursondate));         rates = (valutedatavalutecursondate) xs.deserialize(sr);     }     richtextbox1.text = xml;     // messagebox.show(rates.vname); }  var form = new form1(); try {     form.loaddata(); } catch (exception ex) {     messagebox.show(ex.tostring());     throw; } 

Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -