简体   繁体   中英

Deserialize XML into Entitylist using C# from XMLdocument

Refer the below XML File with multiple event nodes into 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
    {
        get
        {
            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 to deserlize the menioned XML into above Entity class

pDoc is the object of XMLdocument filled with 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 is getting null due to which result object of List is throwing exception.

I want the List of Events.

Please help me to achieve list of events. let me know if need to provide further details.

I can't really see how this could work. Your Event class seems to have only one 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 is clear, you need to do that way with every available property. Now, deserialization:

        string input = System.IO.File.ReadAllText(PATH_TO_YOUR_XML_FILE); //this can be replaced with any 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, this will produce Events class, with List in EventList. Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM