I've been looking on similar StackOverFlow posts but nothing seems to work.
I'm trying to deserialize an XML string that is being sent to me from a restful service using RestSharper. Basically, my problem is that some of the properties of my model class are being left null, and I have no clue why.
The model class I'm using is this:
class Photo
{
[SerializeAs(Name = "id")]
public long? id { get; set; }
[SerializeAs(Name = "title")]
public string title { get; set; }
[SerializeAs(Name = "description")]
public string description { get; set; }
[SerializeAs(Name = "size")]
public long? size { get; set; }
[SerializeAs(Name = "user")]
public User user { get; set; }
[SerializeAs(Name = "tags")]
public List<Tag> listTags { get; set; }
[SerializeAs(Name = "comments")]
public List<Comment> listComments { get; set; }
}
class Photos
{
[SerializeAs(Name = "photos")]
public List<Photo> listPhotos { get; set; }
}
The XML I'm trying to deserialize follows this structure :
<photos>
<photo>
<id>1</id>
<title>some title...</title>
<description>some description...</description>
<size>162798</size>
<user>
<id>4</id>
<name>John</name>
</user>
<tags>
<tag>
<id>1</id>
<value>some value...</value>
</tag>
</tags>
<comments>
<comment>
<id>1</id>
<value>some comment</value>
<author>admin</author>
</comment>
</comments>
</photo>
</photos>
Back to the C# code. The thing is that the list of comments, the list of tags, and the user object of the Photo class are being left null . Does anyone knows why is this happening?
Related
I have this xml response as a stream from USPS 3.0 zip code look up api
<ZipCodeLookupResponse>
<Address ID="1">
<Address1>Unit 2</Address1>
<Address2>8 WILDWOOD DR</Address2>
<City>OLD LYME</City>
<State>CT</State>
<Zip5>06371</Zip5>
</Address>
<Address ID="2">
<Address1>Unit 2</Address1>
<Address2>8 WILDWOOD DR</Address2>
<City>OLD LYME</City>
<State>CT</State>
<Zip5>06371</Zip5>
</Address>
</ZipCodeLookupResponse>
and I'm trying to deserialize the response in an array of addresses.
[XmlRoot(ElementName = "ZipCodeLookupResponse")]
public class UspsAddress
{
[XmlArray("Address")]
public UspsAddressResult[] Addresses {get;set;}
}
[XmlRoot(ElementName = "Address")]
public class UspsAddressResult
{
[XmlElement(ElementName = "Address2")]
public string Adress1 { get; set; }
[XmlElement(ElementName = "Address1")]
public string Adress2 { get; set; }
[XmlElement(ElementName = "City")]
public string City { get; set; }
[XmlElement(ElementName = "State")]
public string State { get; set; }
[XmlElement(ElementName = "Zip5")]
public string Zip { get; set; }
}
This code is always returning an empty array. How can I fix this code to get both address from the response?
...
var content = await res.Content.ReadAsStreamAsync();
var serializer = new XmlSerializer(typeof(UspsAddress));
var results = (UspsAddress)serializer.Deserialize(content);
Instead of using XmlArray or XmlArrayItem, you can use the `XmlElement``
attribute to set the name of the child elements. The deserializer will recognize that there are multiple elements that are supposed to be deserialized into a list of objects.
Your class then looks like this:
[XmlRoot(ElementName = "ZipCodeLookupResponse")]
public class UspsAddress
{
[XmlElement("Address")]
public UspsAddressResult[] Addresses {get;set;}
}
I have the following XML:
<results>
<row>
<id>4990321</id>
<field name="First name">John</field>
<field name="Last name">Snow</field>
<field name="Country">USA</field>
<field name="City">San-Diego</field>
<field name="Postcode">123456</field>
<field name="Email">username#stackoverflow.com</field>
<field name="Message">Hello</field>
</row>
<row>
...
<row>
</results>
I need to deserialize it in array of objects:
public class Employee
{
public string Id { set; get; }
public string Firstname { set; get; }
public string Lastname { set; get; }
public string Country { set; get; }
public string City { set; get; }
public string Postcode { set; get; }
public string Email { set; get; }
public string Message { set; get; }
}
I've tried to implement it using XmlElementAttribute and XmlAttributeAttribute, but property named ElementName doesn't understand XPath, so I couldn't get value of specific field (as field[name='Email'])
Who has any ideas how can I do it? Desirable without XDocument parse.
One suggestion would be to pre-process your data into a form that is more friendly to serialize via an XSLT transform. This can be accomplished in C# via the XslCompiledTransform class.
A sample XSL file to use would be:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output encoding="UTF-8" indent="yes" method="xml" standalone="no" omit-xml-declaration="no"/>
<xsl:template match="results">
<Employees>
<xsl:for-each select="row">
<Employee>
<Id><xsl:value-of select="id" /></Id>
<Firstname><xsl:value-of select="field[#name='First name']" /></Firstname>
<Lastname><xsl:value-of select="field[#name='Last name']" /></Lastname>
<Country><xsl:value-of select="field[#name='Country']" /></Country>
</Employee>
</xsl:for-each>
</Employees>
</xsl:template>
</xsl:stylesheet>
(Incomplete but enough for illustration purposes)
In your C# program the transformation is simple:
var xslReader = XmlReader.Create(new StringReader(GetXslString()));
var xmlReader = XmlReader.Create(new StringReader(GetXmlString()));
var transformedStream = new MemoryStream();
var xmlWriter = XmlWriter.Create(transformedStream);
var xslt = new XslCompiledTransform();
xslt.Load(xslReader);
xslt.Transform(xmlReader, xmlWriter);
transformedStream.Seek(0, SeekOrigin.Begin);
// Use your transformedStream to deserialize the object
.NET fiddle demonstrating this code here
When this is run against your sample data, it produces outputs similar to:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Employees>
<Employee>
<Id>4990321</Id>
<Firstname>Jhon</Firstname>
<Lastname>Snow</Lastname>
<Country>USA</Country>
</Employee>
<Employee>
<Id>4990322</Id>
<Firstname>Mike</Firstname>
<Lastname>Ross</Lastname>
<Country>UK</Country>
</Employee>
</Employees>
This should be fairly easy now for the any of the default deserialization tools to take over without advanced document selectors, etc.
You can deserialize it by explicitly declaring the order the elements are deserialized in using Order:
[XmlRoot("results")]
public class Results
{
[XmlElement("row")]
public List<Employee> Employees { get; set; }
}
public class Employee
{
[XmlElement(ElementName = "id", Order = 1)]
public string Id { set; get; }
[XmlElement(ElementName = "field", Order = 2)]
public string Firstname { set; get; }
[XmlElement(ElementName = "field", Order = 3)]
public string Lastname { set; get; }
[XmlElement(ElementName = "field", Order = 4)]
public string Country { set; get; }
[XmlElement(ElementName = "field", Order = 5)]
public string City { set; get; }
[XmlElement(ElementName = "field", Order = 6)]
public string Postcode { set; get; }
[XmlElement(ElementName = "field", Order = 7)]
public string Email { set; get; }
[XmlElement(ElementName = "field", Order = 8)]
public string Message { set; get; }
}
You can then deserialize this as such (where xml is your XML string):
var results = new XmlSerializer(typeof(Results)).Deserialize(new StringReader(xml));
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>"
I need some help with XmlSerializer. I have to following xml fragment:
<?xml version='1.0' encoding='UTF-8'?>
<?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?>
<feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'>
<id>tag:blogger.com,1999:blog-4233645339430781865.archive</id>
<updated>2012-10-22T07:00:02.139+03:00</updated>
<title type='text'>Code !t</title>
<link rel='alternate' type='text/html' href='http://www.etabakov.com/'/>
<author>
<name>Емил Табаков</name>
<email>noreply#blogger.com</email>
<gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh5.googleusercontent.com/-TbRwL19G85U/AAAAAAAAAAI/AAAAAAAAFxg/NRV6ZYqd9Wg/s512-c/photo.jpg'/>
</author>
<generator version='7.00' uri='http://www.blogger.com'>Blogger</generator>
<entry>
<id>tag:blogger.com,1999:blog-4233645339430781865.post-513753811167440871</id>
<published>2012-10-12T11:22:35.759+03:00</published>
<updated>2012-10-12T11:22:35.759+03:00</updated>
<category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/blogger/2008/kind#comment'/>
<title type='text'>Great post indeed. I really like that you are prov...</title>
<content type='html'>Great post indeed. I really like that you are providing information on .NET for freshers , Being enrolled at http://www.wiziq.com/course/57-fresher-training-projects i found your information very helpful indeed. Thanks for it.</content>
<link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4233645339430781865/8317071019326278340/comments/default/513753811167440871'/>
<author>
<name>sarabjeet</name>
<uri>http://www.blogger.com/profile/11223974173581186160</uri>
<email>noreply#blogger.com</email>
<gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/>
</author>
<thr:in-reply-to href='http://www.etabakov.com/2012/06/net-guy-velocityconf-2012-day-1.html' ref='tag:blogger.com,1999:blog-4233645339430781865.post-8317071019326278340' source='http://www.blogger.com/feeds/4233645339430781865/posts/default/8317071019326278340' type='text/html'/>
<gd:extendedProperty name='blogger.itemClass' value='pid-899300522'/>
</entry>
</feed>
And I also have the following c# objects:
Feed.cs
[XmlRoot(ElementName = "feed", Namespace = "http://www.w3.org/2005/Atom"), XmlType("feed")]
public class Feed
{
[XmlElement("id")]
public string Id { get; set; }
[XmlElement("title")]
public string Title { get; set; }
[XmlElement("author")]
public Author Author { get; set; }
[XmlElement("entry")]
public List<Entry> Entry;
}
public class Entry
{
[XmlElement("id")]
public string Id { get; set; }
[XmlElement("title")]
public string Title { get; set; }
[XmlElement("content")]
public string Content { get; set; }
[XmlElement("published")]
public DateTime Published { get; set; }
[XmlElement("updated")]
public DateTime Updated { get; set; }
[XmlElement("category")]
public List<Category> Categories;
[XmlElement("author")]
public Author Author { get; set; }
[XmlElement(ElementName = "in-reply-to", Namespace = "thr", Type = typeof(ReplyTo), IsNullable = true)]
public ReplyTo ReplyTo { get; set; }
}
public class ReplyTo
{
[XmlAttribute("ref")]
public string Id { get; set; }
}
Everything works perfectly so far, except that ReplyTo property always stays null. I need to get the src attribute from the
I will be really happy if someone show me what I'm missing. Thanks!
The namespace you need is "http://purl.org/syndication/thread/1.0"
"thr" is just the alias - as declared by the xmlns:thr at the top.
So:
[XmlElement(ElementName = "in-reply-to", Namespace = "http://purl.org/syndication/thread/1.0", Type = typeof(ReplyTo), IsNullable = true)]
public ReplyTo ReplyTo { get; set; }
I need some help with XmlSerializer. I have to following xml fragment:
<?xml version='1.0' encoding='UTF-8'?>
<?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?>
<feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'>
<id>tag:blogger.com,1999:blog-4233645339430781865.archive</id>
<updated>2012-10-22T07:00:02.139+03:00</updated>
<title type='text'>Code !t</title>
<link rel='alternate' type='text/html' href='http://www.etabakov.com/'/>
<author>
<name>Емил Табаков</name>
<email>noreply#blogger.com</email>
<gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh5.googleusercontent.com/-TbRwL19G85U/AAAAAAAAAAI/AAAAAAAAFxg/NRV6ZYqd9Wg/s512-c/photo.jpg'/>
</author>
<generator version='7.00' uri='http://www.blogger.com'>Blogger</generator>
<entry>
<id>tag:blogger.com,1999:blog-4233645339430781865.post-513753811167440871</id>
<published>2012-10-12T11:22:35.759+03:00</published>
<updated>2012-10-12T11:22:35.759+03:00</updated>
<category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/blogger/2008/kind#comment'/>
<title type='text'>Great post indeed. I really like that you are prov...</title>
<content type='html'>Great post indeed. I really like that you are providing information on .NET for freshers , Being enrolled at http://www.wiziq.com/course/57-fresher-training-projects i found your information very helpful indeed. Thanks for it.</content>
<link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4233645339430781865/8317071019326278340/comments/default/513753811167440871'/>
<author>
<name>sarabjeet</name>
<uri>http://www.blogger.com/profile/11223974173581186160</uri>
<email>noreply#blogger.com</email>
<gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/>
</author>
<thr:in-reply-to href='http://www.etabakov.com/2012/06/net-guy-velocityconf-2012-day-1.html' ref='tag:blogger.com,1999:blog-4233645339430781865.post-8317071019326278340' source='http://www.blogger.com/feeds/4233645339430781865/posts/default/8317071019326278340' type='text/html'/>
<gd:extendedProperty name='blogger.itemClass' value='pid-899300522'/>
</entry>
</feed>
And I also have the following c# objects:
Feed.cs
[XmlRoot(ElementName = "feed", Namespace = "http://www.w3.org/2005/Atom"), XmlType("feed")]
public class Feed
{
[XmlElement("id")]
public string Id { get; set; }
[XmlElement("title")]
public string Title { get; set; }
[XmlElement("author")]
public Author Author { get; set; }
[XmlElement("entry")]
public List<Entry> Entry;
}
public class Entry
{
[XmlElement("id")]
public string Id { get; set; }
[XmlElement("title")]
public string Title { get; set; }
[XmlElement("content")]
public string Content { get; set; }
[XmlElement("published")]
public DateTime Published { get; set; }
[XmlElement("updated")]
public DateTime Updated { get; set; }
[XmlElement("category")]
public List<Category> Categories;
[XmlElement("author")]
public Author Author { get; set; }
[XmlElement(ElementName = "in-reply-to", Namespace = "thr", Type = typeof(ReplyTo), IsNullable = true)]
public ReplyTo ReplyTo { get; set; }
}
public class ReplyTo
{
[XmlAttribute("ref")]
public string Id { get; set; }
}
Everything works perfectly so far, except that ReplyTo property always stays null. I need to get the src attribute from the
I will be really happy if someone show me what I'm missing. Thanks!
The namespace you need is "http://purl.org/syndication/thread/1.0"
"thr" is just the alias - as declared by the xmlns:thr at the top.
So:
[XmlElement(ElementName = "in-reply-to", Namespace = "http://purl.org/syndication/thread/1.0", Type = typeof(ReplyTo), IsNullable = true)]
public ReplyTo ReplyTo { get; set; }