.net - Deserializing XML into class obj in C# -
this 1 of entries have in huge xml file of on 200 such entries.
<tradefills> <tradefill> <broker>xxx</broker> <customeraccount/> <qwfilltransid>xxxxxxxxxxxxxxxxxxx</qwfilltransid> <qwprevtransid>xxx</qwprevtransid> <qwgrouptransid>xxxxxxxxxxxxxxxxx</qwgrouptransid> <grouptransid>xxxxxxxx</grouptransid> <transid>x</transid> <service>xxxxxxxxxxxxxxxx</service> <symbol>xx</symbol> <exchange>xxxxx</exchange> <instclass>xxxxxxxx</instclass> <instsubclass>xxxxxxx</instsubclass> <contractsymbol>xxxx</contractsymbol> <expirationdate>xxxxxxxx</expirationdate> <month>xx</month> <year>xxxx</year> <strike>xxx</strike> <tradepcu>xxxx</tradepcu> <buy>x</buy> <quantity>xx</quantity> <price>xxxxx</price> <filltime>xxxxxxxxxxxxxxx</filltime> <posupdated>xxxxxxxxxxx</posupdated> <description/> </tradefill> </tradefills>
i attempting deserialize class object failing every time.
this code far:
using system;
using system.collections.generic; using system.text; using system.io; using system.xml.serialization;
namespace deserializexml { public class program {
// class deserialized. [serializable()] public class tradefill { [xmlelement("broker")] public string broker; [xmlelement("qwfilltransid")] public string qwfilltransid; [xmlelement("qwprevtransid")] public string qwprevtransid; [xmlelement("qwgrouptransid")] public string qwgrouptransid; [xmlelement("grouptransid")] public string grouptransid; [xmlelement("transid")] public string transid; [xmlelement("service")] public string service; [xmlelement("exchange")] public string exchange; [xmlelement("instclass")] public string instclass; [xmlelement("instsubclass")] public string instsubclass; [xmlelement("contractsymbol")] public string consymbol; [xmlelement("expirationdate")] public datetime expdate; [xmlelement("month")] public int month; [xmlelement("year")] public int year; [xmlelement("strike")] public double strike; [xmlelement("tradepcu")] public string tradepcu; [xmlelement("buy")] public int buy; [xmlelement("quantity")] public int quantity; [xmlelement("price")] public double price; [xmlelement("filltime")] public datetime filltime; [xmlelement("posupdated")] public string posupdated; } [xmlrootattribute("tradefills")] public class sigtrades { [xmlelement("tradefills")] public tradefill[] tradefills{ get; set; } } [serializable()] public class test { public static void main() { test t = new test(); // read purchase order. t.deserializeobject("c:\\test.xml"); } private void deserializeobject(string filename) { console.writeline("reading stream"); // create instance of xmlserializer. xmlserializer serializer = new xmlserializer(typeof(tradefill)); // reading xml document requires filestream. stream reader= new filestream(filename,filemode.open); // declare object variable of type deserialized. tradefill i; // call deserialize method restore object's state. = (tradefill)serializer.deserialize(reader); // write out properties of object. console.write(i.qwfilltransid); } } }
}
when execute it, see "reading stream" on console, , nothing else.
edit
i realized 'reading stream" because still reading. after 5 seconds, got:
unhandled exception: system.invalidoperationexception: deserializexml.program inaccessible due protection level. public types can processed. @ system.xml.serialization.typedesc.checksupported() @ system.xml.serialization.typescope.gettypedesc(type type, memberinfo sourc e, boolean directreference, boolean throwonerror) @ system.xml.serialization.typescope.importtypedesc(type type, memberinfo me mberinfo, boolean directreference) @ system.xml.serialization.typescope.gettypedesc(type type, memberinfo sourc e, boolean directreference, boolean throwonerror) @ system.xml.serialization.modelscope.gettypemodel(type type, boolean direct reference) @ system.xml.serialization.xmlreflectionimporter.importtypemapping(type type , xmlrootattribute root, string defaultnamespace) @ system.xml.serialization.xmlserializer..ctor(type type, string defaultname space) @ system.xml.serialization.xmlserializer..ctor(type type) @ deserializexml.program.test.deserializeobject(string filename) in c:\docum ents , settings\sobti\my documents\visual studio 2010\projects\deserializexml\ deserializexml\program.cs:line 109 @ deserializexml.program.test.main() in c:\documents , settings\sobti\my d ocuments\visual studio 2010\projects\deserializexml\deserializexml\program.cs:li ne 102
edit
after making program class public, error:
unhandled exception: system.invalidoperationexception: there error reflec ting type 'deserializexml.program.tradefill'. ---> system.invalidoperationexcept ion: there error reflecting field 'grouptransid'. ---> system.invalidoper ationexception: xml element 'qwgrouptransid' namespace '' pr esent in current scope. use xml attributes specify xml name or na mespace element. @ system.xml.serialization.xmlreflectionimporter.adduniqueaccessor(inamescop e scope, accessor accessor) @ system.xml.serialization.xmlreflectionimporter.adduniqueaccessor(membermap ping member, inamescope elements, inamescope attributes, boolean issequence) @ system.xml.serialization.xmlreflectionimporter.initializestructmembers(str uctmapping mapping, structmodel model, boolean openmodel, string typename, recur sionlimiter limiter)
edit
made bunch of changes code (as reflected above). had items listed twice, double checked case etc. error:
unhandled exception: system.invalidoperationexception: there error in xml document (2, 2). ---> system.invalidoperationexception: <tradefills xmlns=''> wa s not expected. @ microsoft.xml.serialization.generatedassembly.xmlserializationreadertradef ill.read3_tradefill() --- end of inner exception stack trace ---
- element
qwgrouptransid
mentioned twice. have in own element , name on grouptransid, deserializer doesn't know put values it. - your value
filltime
invalid. - you're trying deserialize
tradefill
, actual xmlcarcollection
.
Comments
Post a Comment