Deserialize XML into Entitylist using C# from XMLdocument - c#

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.

Related

XML deserialization returns list with one empty item

I have the following XML:
<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>
<accounts>
<items>
<account>
<voornaam><FirstName</voornaam>
<naam>LastName</naam>
<gebruikersnaam>Username</gebruikersnaam>
<internnummer></internnummer>
<klasnummer></klasnummer>
</account>
</items>
</accounts>
With these classes:
public class Accounts
{
public Accounts()
{
items = new List<Account>();
}
[XmlElement]
public List<Account> items { get; set; }
}
public class Account
{
public string voornaam { get; set; }
public string naam { get; set; }
public string gebruikersnaam { get; set; }
public int? internnummer { get; set; }
public int? klasnummer { get; set; }
}
And this code:
var decoded = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><accounts><items><account><voornaam>FirstName</voornaam><naam>LastName</naam><gebruikersnaam></gebruikersnaam><internnummer></internnummer><klasnummer></klasnummer></account></items></accounts>";
XmlSerializer serializer = new XmlSerializer(typeof(Accounts), new XmlRootAttribute("accounts"));
StringReader reader = new StringReader(decoded);
var accounts = (Accounts)serializer.Deserialize(reader);
All I get from this is an Accounts instance with the property Items containing one Account instance with every property null.
You need to specify the items as XML array with XmlArrayAttribute and also with the XmlArrayItemAttribute with (element) item's name: "account" and as Account type. XmlArrayAttribute Example.
public class Accounts
{
[XmlArrayItem(ElementName= "account",
Type = typeof(Account))]
[XmlArray(ElementName="items")]
public List<Account> items { get; set; }
}
Meanwhile, suggest using XmlElementAttribute to specify the element name in XML instead of naming the properties as camelCase.
For the reason why need InternnummerText and KlasnummerText properties you can refer to the question: Deserializing empty xml attribute value into nullable int property using XmlSerializer (will not cover in this answer).
public class Accounts
{
public Accounts()
{
Items = new List<Account>();
}
[XmlArrayItem(ElementName= "account",
Type = typeof(Account))]
[XmlArray(ElementName="items")]
public List<Account> Items { get; set; }
}
[XmlRoot(ElementName="account")]
public class Account
{
[XmlIgnore]
public int? Klasnummer { get; set; }
[XmlIgnore]
public int? Internnummer { get; set; }
[XmlElement(ElementName="voornaam")]
public string Voornaam { get; set; }
[XmlElement(ElementName="naam")]
public string Naam { get; set; }
[XmlElement(ElementName="gebruikersnaam")]
public string Gebruikersnaam { get; set; }
[XmlElement(ElementName="internnummer")]
public string InternnummerText
{
get { return (Internnummer.HasValue) ? Internnummer.ToString() : null; }
set { Internnummer = !string.IsNullOrEmpty(value) ? int.Parse(value) : default(int?); }
}
[XmlElement(ElementName="klasnummer")]
public string KlasnummerText
{
get { return (Klasnummer.HasValue) ? Klasnummer.ToString() : null; }
set { Klasnummer = !string.IsNullOrEmpty(value) ? int.Parse(value) : default(int?); }
}
}
Sample program

Deserializing XML with different Namespaces UWP

I have following XML-Data:
<?xml version="1.0"?>
<e:propertyset xmlns:e="urn:schemas-upnp-org:event-1-0">
<e:property>
<LastChange>
<Event xmlns="urn:schemas-upnp-org:metadata-1-0/RCS/">
<InstanceID val="0">
<RoomVolumes val="uuid:29e07ad9-224f-4160-a2bc-61d17845182a=100"/>
<Volume channel="Master" val="100"/>
<Mute channel="Master" val="0"/>
<RoomMutes val="uuid:29e07ad9-224f-4160-a2bc-61d17845182a=0"/>
</InstanceID>
</Event>
</LastChange>
</e:property>
</e:propertyset>
And here are my classes:
[XmlRoot("propertyset", Namespace = "urn:schemas-upnp-org:event-1-0")]
public class EventPropertySet
{
[XmlElement("property")]
public List<EventProperty> Properties { get; set; }
}
public class EventProperty
{
[XmlElement("LastChange")]
public string LastChange { get; set; }
[XmlElement("SinkProtocolInfo")]
public string SinkProtocolInfo { get; set; }
[XmlElement("IndexerStatus")]
public string IndexerStatus { get; set; }
[XmlElement("SystemUpdateID")]
public string SystemUpdateID { get; set; }
}
Now when I try to deserialize the XML-Data 'LastChange' is always 'null'.
When I modify the class 'EventProperty' like so:
public class EventProperty
{
[XmlElement("LastChange", Namespae = "")]
public string LastChange { get; set; }
[XmlElement("SinkProtocolInfo", Namespae = "")]
public string SinkProtocolInfo { get; set; }
[XmlElement("IndexerStatus", Namespae = "")]
public string IndexerStatus { get; set; }
[XmlElement("SystemUpdateID", Namespae = "")]
public string SystemUpdateID { get; set; }
}
The deserialization throws an exception:
XmlException: ReadElementContentAs() methods cannot be called on an element that has child elements. Line 1, position 103.
Any ideas what I should do?
Sorry, I found the problem. The data behind "LastChange" is normaly a parsed XML-Structure (like this:)
<LastChange><Event xmlns="urn:schemas-upnp-org:metadata-1-0/RCS/"><InstanceID val="0"><RoomVolumes val="uuid:29e07ad9-224f-4160-a2bc-61d17845182a=100"/><Volume channel="Master" val="100"/><Mute channel="Master" val="0"/><RoomMutes val="uuid:29e07ad9-224f-4160-a2bc-61d17845182a=0"/></InstanceID></Event></LastChange>
Now when I don't do the "DeParse" with WebUtility.HtmlDecode, everythings works fine.

XML Deserializing returns "some" null values

Here's my XML:
<Events>
<Event>
<content_id>6442452774</content_id>
<content_title>Title of the event</content_title>
<content_html>
<Date>2015-11-18</Date>
<EventType>Events</EventType>
<Description>
<p>this is an "event"</p>
</Description>
<Speakers>speaker1 LLC<br />speaker2<br />Jspeaker3</Speakers>
<Time>5:30 - 6:00pm Registration<br />6:00 - 7:00pm Panel Discussion<br />7:00 - 8:00pm Networking Reception</Time>
<Where></Where>
<RegistrationInfo>Please contact events#events.com to register for this event.</RegistrationInfo>
<Registration>false</Registration>
</content_html>
<date_created>2015-10-24T14:24:12.333</date_created>
<folder_id>262</folder_id>
<content_teaser>this is the content "can you display it."</content_teaser>
<content_text>text of the vent "more text" a lot of text here </content_text>
<end_date>2015-11-19T21:35:00</end_date>
<content_type>1</content_type>
<template_id>43</template_id>
<content_status>A</content_status>
</Event>
<Event>.... Other events </Event>
<Events>
and here's are my classes:
public class Serializable_Events
{
[XmlElement("Event")]
public List<Serializable_Event> EventList = new List<Serializable_Event>();
}
public class Serializable_Event
{
[XmlElement("content_id")]
public string content_id { get; set; }
[XmlElement("content_title")]
public string content_title { get; set; }
[XmlElement("content_html")]
public Serializable_Event_ContentHTML ContentHTML { get; set; }
[XmlText]
public string content_teaser { get; set; }
[XmlElement("content_text")]
public string content_text { get; set; }
}
public class Serializable_Event_ContentHTML
{
[XmlElement("Date")]
public string Date { get; set; }
[XmlElement("EventType")]
public string EventType { get; set; }
[XmlElement("Description")]
public string Description { get; set; }
[XmlElement("Speakers")]
public string Speakers { get; set; }
[XmlElement("Time")]
public string Time { get; set; }
[XmlElement("Where")]
public string Where { get; set; }
[XmlElement("RegistrationInfo")]
public string RegistrationInfo { get; set; }
[XmlElement("Registration")]
public bool Registration { get; set; }
//ignored html tags
[XmlIgnore]
public string p { get; set; }
[XmlIgnore]
public string br { get; set; }
[XmlIgnore]
public string a { get; set; }
}
Implementation:
XmlSerializer ser = new XmlSerializer(typeof(Serializable_Events));
var data = (Serializable_Events)ser.Deserialize(new StreamReader(#"events.xml"));
My problem is that some attributes are null and some are not (see the screenshot)
The ones that are null, like <Description> are due to malformed
XML.
You are storing HMTL directly in XML with text all over the place, and the serializer is not expecting that; further you are telling the serializer to ignore HTML tags using XmlIgnore which is intended for XML tags with properly formed XML. That's a wrong use of XmlIgnore
All XML which contains non-XML mark-up should be wrapped in CDATA sections; this will solve your problem. Further, you can remove all of the XmlIgnore code as well since it's not needed.
Your XML should look like this:
<Events>
<Event>
<content_id>6442452774</content_id>
<content_title>Title of the event</content_title>
<content_html>
<Date>2015-11-18</Date>
<EventType>Events</EventType>
<Description>
<![CDATA[<p>this is an ""event""</p>]]>
</Description>
<Speakers>
<![CDATA[speaker1 LLC<br />speaker2<br />Jspeaker3]]>
</Speakers>
<Time>
<![CDATA[5:30 - 6:00pm Registration<br />6:00 - 7:00pm Panel Discussion<br />7:00 - 8:00pm Networking Reception]]>
</Time>
<Where></Where>
<RegistrationInfo>
<![CDATA[Please contact <a href='mailto:events#events.com'>events#events.com</a> to register for this event.]]>
</RegistrationInfo>
<Registration>false</Registration>
</content_html>
<date_created>2015-10-24T14:24:12.333</date_created>
<folder_id>262</folder_id>
<content_teaser>this is the content 'can you display it.'</content_teaser>
<content_text>text of the vent 'more text' a lot of text here </content_text>
<end_date>2015-11-19T21:35:00</end_date>
<content_type>1</content_type>
<template_id>43</template_id>
<content_status>A</content_status>
</Event>
</Events>"

Xml Deserialize returms null values but xml has values

I saw few topics but no one looks like my problem.
Here is my class:
namespace Framework.Cielo.Models
{
[XmlRoot("transacao", Namespace = "http://ecommerce.cbmp.com.br")]
public class TransactionResponse
{
[XmlAttribute("id")]
public string ID { get; set; }
[XmlAttribute("versao")]
public string Version { get; set; }
[XmlElement("tid")]
public string tid { get; set; }
[XmlElement("pan")]
public string pan { get; set; }
[XmlElement("dados-pedido")]
public EstablishmentOrder Order { get; set; }
[XmlElement("forma-pagamento")]
public PaymentMethod PaymentMethod { get; set; }
[XmlElement("status")]
public TransactionStatusEnum Status { get; set; }
[XmlElement("url-retorno")]
public string ReturnUrl { get; set; }
[XmlElement("autenticacao")]
public Authentication Authentication { get; set; }
}
}
and here is the authentication class
namespace Framework.Cielo.Models
{
[XmlRoot("autenticacao")]
public class Authentication
{
[XmlElement("codigo")]
public int Code { get; set; }
[XmlElement("mensagem")]
public string Message { get; set; }
[XmlIgnore]
public DateTime Date { get; set; }
[XmlElement("data-hora")]
public string FormattedDate
{
get
{
return Date.ToString("yyyy-MM-ddTHH:mm:ss");
}
set
{
DateTime kdc = DateTime.MinValue;
DateTime.TryParse(value, out kdc);
Date = kdc;
}
}
[XmlElement("valor")]
public int Value { get; set; }
[XmlElement("lr")]
public int SecurityLevel { get; set; }
[XmlElement("arp")]
public object arp { get; set; }
[XmlElement("nsu")]
public object nsu { get; set; }
}
}
Here is how I deserialize:
string serializado = File.ReadAllText("req.xml");
var stringReader = new System.IO.StringReader(serializado);
var serializer = new XmlSerializer(typeof(TransactionResponse));
TransactionResponse preAuthResponse = serializer.Deserialize(stringReader) as TransactionResponse;
and here is my XML:
<?xml version="1.0" encoding="ISO-8859-1"?>
<transacao versao="1.3.0" id="100" xmlns="http://ecommerce.cbmp.com.br">
<tid>10069930690A16DF1001</tid>
<pan>b1SQ6jpKCDt3n9C0dgD/ZkPQ1Bh+7aJESqr/CwP64P0=</pan>
<dados-pedido>
<numero>100</numero>
<valor>29900</valor>
<moeda>986</moeda>
<data-hora>2013-10-15T00:57:19.032-03:00</data-hora>
<descricao/>
<idioma>PT</idioma>
<taxa-embarque>0</taxa-embarque>
</dados-pedido>
<forma-pagamento>
<bandeira>mastercard</bandeira>
<produto>1</produto>
<parcelas>1</parcelas>
</forma-pagamento>
<status>4</status>
<autenticacao>
<codigo>4</codigo>
<mensagem>Transacao sem autenticacao</mensagem>
<data-hora>2013-10-15T00:57:19.037-03:00</data-hora>
<valor>29900</valor>
<eci>0</eci>
</autenticacao>
<autorizacao>
<codigo>4</codigo>
<mensagem>Transação autorizada</mensagem>
<data-hora>2013-10-15T00:57:19.041-03:00</data-hora>
<valor>29900</valor>
<lr>00</lr>
<arp>123456</arp>
<nsu>661215</nsu>
</autorizacao>
</transacao>
When I run this code, all the elements get right, but not ARP and NSU elements (the last 2 of autorizacao tag)
I really don't know why. This XML comes from a web service and I can't figure out why my deserialize don't work with the 2 last items but works greater with any other element.
I have tried with your code and updated following and it works.
Commented second last <autenticacao> tag.
Rename last tag <autorizacao> to <autenticacao> . May be you are getting wrong xml & last two tags are confusing so I have tried with only one tag.
In Authentication class I have changed type of ARP and NSU otherwise we are getting XMlNode type while deserializing. You can also use string instead of int.
[XmlElement("arp")]
public int arp { get; set; }
[XmlElement("nsu")]
public int nsu { get; set; }

Deserialize XML with multiple namespaces to objects

I´m trying to deserialize this XML to objects in C# .NET 4.5:
<DIDL-Lite xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:upnp="urn:schemas-upnp-org:metadata-1-0/upnp/"
xmlns="urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/">
<item id="28" parentID="19" restricted="1">
<dc:creator>Alicia Keys</dc:creator>
<dc:date>2003-01-01</dc:date>
<dc:title>Gangsta Lovin&apos; (feat. Alicia Keys)</dc:title>
</item>
</DIDL-Lite>
Code:
I´m not getting any "item" Lists. The object isn't deserialized.
MemoryStream reader = new MemmoryStream(System.Text.Encoding.Unicode.GetBytes(Result));
var ser = new XmlSerializer(typeof(DIDLLite));
DIDLLite device = (DIDLLite)ser.Deserialize(reader);
Class DIDLLite:
[XmlRoot("DIDL-Lite", Namespace = "urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/")]
public class DIDLLite {
DIDLLite() {
this.serviceItem = new List<ContainerItem>();
}
[System.Xml.Serialization.XmlArrayItem("item", typeof(ServiceListTypeService), IsNullable = false)]
List<ContainerItem> serviceItem = new List<ContainerItem>();
}
Class ContainerItem:
public class ContainerItem
{
[System.Xml.Serialization.XmlAttribute("id")]
public string id { get; set; }
[System.Xml.Serialization.XmlAttribute("parentID")]
public string parentID { get; set; }
[System.Xml.Serialization.XmlAttribute("restricted")]
public string restricted { get; set; }
[System.Xml.Serialization.XmlAttribute("searchable")]
public string searchable { get; set; }
public string title { get; set; }
}
You have several issues:
you define an XmlArrayItem attribute - but really, in your XML, you don't have any list of items. If you want to use an Xml array construct, you'd need to have something like this for your XML:
<DIDL-Lite .....>
<Items>
<item id="28" parentID="19" restricted="1">
......
</item>
<item id="29" parentID="19" restricted="1">
......
</item>
</Items>
</DIDL-Lite>
So you would need to have an <Items>...</Items> wrapper around your <item> elements.
You have this declaration:
[XmlArrayItem("item", typeof(ServiceListTypeService), IsNullable = false)]
but where is the ServiceListtypeService defined? I don't see any trace of it....
I simplified your code a bit - and this works just fine:
[XmlRoot("DIDL-Lite", Namespace = "urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/")]
public class DIDLLite
{
[XmlElement("item")]
public ContainerItem Item { get; set; }
}
public class ContainerItem
{
[XmlAttribute("id")]
public string id { get; set; }
[XmlAttribute("parentID")]
public string parentID { get; set; }
[XmlAttribute("restricted")]
public string restricted { get; set; }
[XmlAttribute("searchable")]
public string searchable { get; set; }
// you were missing these elements and their namespace
[XmlElement(Namespace = "http://purl.org/dc/elements/1.1/")]
public string creator { get; set; }
[XmlElement(Namespace = "http://purl.org/dc/elements/1.1/")]
public string date { get; set; }
[XmlElement(Namespace = "http://purl.org/dc/elements/1.1/")]
public string title { get; set; }
}
And now, when I run your code to deserialize your XML, I do get the objects filled nicely.

Categories