[英]Deserialize XML into Entitylist using C# from XMLdocument
Refer the below XML File with multiple event nodes into parent events tag 将具有多个事件节点的以下XML文件引用到父事件标签中
<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 我使用下面的代码将提到的XML反序列化为上面的Entity类
pDoc is the object of XMLdocument filled with above xml pDoc是填充以上xml的XMLdocument的对象
System.Xml.Serialization.XmlRootAttribute xRoot = new System.Xml.Serialization.XmlRootAttribute(); System.Xml.Serialization.XmlRootAttribute xRoot =新的System.Xml.Serialization.XmlRootAttribute(); xRoot.ElementName = "events";
xRoot.ElementName =“事件”; xRoot.IsNullable = true;
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. 在上面的代码中,由于List的哪个结果对象引发异常,读取器对象变为null。
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.
您的Event类似乎只有一个事件,XML文件包含许多事件。 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. 好的,这将产生Event类,在EventList中具有List。 Hope this helps.
希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.