Deserialize XML into Entitylist using C# from XMLdocument -
refer below xml file multiple event nodes parent events tag
<events> <event eventid="ahjy67kl" sessionid="1"> <eventtitle> <![cdata[ phoneeventchad_1 ]]> </eventtitle> <eventabstract/> <timezone>ind</timezone> <eventtimedate>wed, 24 jun 2015 06:00 pdt</eventtimedate> <archivestartdate>wed, 24 jun 2015 09:30 pdt</archivestartdate> <archiveenddate>thu, 23 jun 2016 09:30 pdt</archiveenddate> <length>195</length> <sponsor/> <keywords/> <location/> <eventprofile> <![cdata[ stonehenge profile (918) ]]> </eventprofile> <streamtype> <![cdata[ whitepaper ]]> </streamtype> <categories/> <eventstdfield1> <value/> </eventstdfield1> <eventstdfield2/> <eventstdfield3/> <eventstdfield4/> <eventstdfield5/> <audiencekeylock> <![cdata[ 770d14c9cc784e9d9d312563b093e9a5 ]]> </audiencekey> <urls> <audienceurl> <![cdata[ http://event.on24.com/wcc/r/1012538/770d14c9cc784e9d9d312563b093e9a5&partnerref=rss-scribev3 ]]> </audienceurl> <extaudienceurl/> <reporturl> <![cdata[ ##report_url## ]]> </reporturl> <uploadurl> <![cdata[ ##upload_url## ]]> </uploadurl> <presenterurl> <![cdata[ ##presenter_url## ]]> </presenterurl> </urls> <speakers/> <registrationstats> <registrantcount>0</registrantcount> </registrationstats> <attendancestats> <attendedcount>0</attendedcount> <noshowcount>0</noshowcount> <attendedlivecount>0</attendedlivecount> <attendedarchivecount>0</attendedarchivecount> </attendancestats> <partnerrefstats/> <tags/> <registrants></registrants> <attendees></attendees> </event> </events>
"
entity class
public class events { private event _event; [xmlelement(elementname = "event")] public event event { { return this._event; } set { this._event = value; } } } [system.xml.serialization.xmltypeattribute(anonymoustype = true)] public class event { public string eventid { get; set; } public string sessionid { get; set; } public string eventtitle { get; set; } public datetime archivestartdate { get; set; } public datetime archiveenddate { get; set; } public string eventabstract { get; set; } public datetime eventtimedate { get; set; } public string eventprofile { get; set; } public string registrantcount { get; set; } public string sponsor { get; set; } public string keywords { get; set; } public string eventstdfield1 { get; set; } public string eventstdfield2 { get; set; } public string eventstdfield3 { get; set; } public string eventstdfield4 { get; set; } public string eventstdfield5 { get; set; } public attendancestats attendancestats { get; set; } public registrationstats registrationstats { get; set; } public event() { } } public class attendancestats { public string attendedcount { get; set; } public string noshowcount { get; set; } public string attendedlivecount { get; set; } public string attendedarchivecount { get; set; } } public class registrationstats { public string registrantcount { get; set; } }
i used below code deserlize menioned xml above entity class
pdoc object of xmldocument filled above xml
system.xml.serialization.xmlrootattribute xroot = new system.xml.serialization.xmlrootattribute(); xroot.elementname = "events"; xroot.isnullable = true;
system.xml.serialization.xmlserializer serializer = new system.xml.serialization.xmlserializer(typeof(list<events>),xroot); xdocument doc = new xdocument(); doc = xdocument.parse(pdoc.outerxml); //system.xml.xmlreader reader = doc.createreader(); xmlreader reader = xmlreader.create(new system.io.stringreader(doc.tostring())); list<event> result = (list<event>)serializer.deserialize(reader); reader.close();
in above code reader object getting null due result object of list throwing exception.
i want list of events.
please me achieve list of events. let me know if need provide further details.
i can't see how work. event class seems have 1 event, xml file contains many events. try this:
[serializable] [xmlroot(elementname = "events")] public class events { public events() { eventlist = new list<event>(); } [xmlelement(elementname="event")] list<event> eventlist {get; set;} } [serializable] public class event { [xmlattribute("eventid") public string eventid {get; set;} ....... [xmlelement(elementname="timezone")] public string timezone {get; set;} }
it clear, need way every available property. now, deserialization:
string input = system.io.file.readalltext(path_to_your_xml_file); //this can replaced func giving string xmlserializer xmlserializer = new xmlserializer(typeof(events)); var doc = xdocument.parse(input); using (var reader = doc.root.createreader()) { return (events)xmlserializer.deserialize(reader); }
okay, produce events class, list in eventlist. hope helps.
Comments
Post a Comment