Error Deserializing an Xml to Object [duplicate] - c#

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; }

Related

XML to object using YAXLib library

there is an xml of the form below, tell me how to deserialize it to get an object. using the YAXLib library
<catalog>
<categories>
<category id="1">category 1</category>
<category id="59349641">category 2</category>
<category id="303608809">category 3</category>
<category id="303614009">category 4</category>
<category id="303643009">category 5</category>
</categories>
</catalog>
how to get object from this xml ml with attributes
I tried this class but nothing is parsed
public class Сatalog
{
public List<Category> categories { get; set; }
}
public class Category
{
[YAXSerializeAs("id")]
public int Id { get; set; }
[YAXElementFor("category")]
public string category { get; set; }
[YAXSerializeAs("parentId")]
public int ParentId { get; set; }
}
tried like this
public class Сatalog
{
public List<string> categories { get; set; }
}
so we get only the text category
For anyone stumbling across a similar problem:
Here is the code to de/serialize:
[Test]
public void Xml_To_Object_YAXLib()
{
const string xml = #"<catalog>
<categories>
<category id=""1"">category 1</category>
<category id=""59349641"">category 2</category>
<category id=""303608809"">category 3</category>
<category id=""303614009"">category 4</category>
<category id=""303643009"">category 5</category>
</categories>
</catalog>";
var serializer = new YAXSerializer(typeof(Catalog),new SerializerOptions
{
ExceptionHandlingPolicies = YAXExceptionHandlingPolicies.ThrowWarningsAndErrors,
ExceptionBehavior = YAXExceptionTypes.Error,
SerializationOptions = YAXSerializationOptions.DontSerializeNullObjects
});
var deserialized = (Catalog) serializer.Deserialize(xml);
var serialized = serializer.Serialize(deserialized);
Assert.That(serialized, Is.EqualTo(xml));
Assert.That(deserialized.Categories.Count, Is.EqualTo(5));
Assert.That(deserialized.Categories[4].Id, Is.EqualTo(303643009));
}
And this is how to decorate classes with attributes to instruct the YAXSerializer:
[YAXSerializeAs("catalog")]
public class Catalog
{
[YAXSerializeAs("categories")]
public List<Category> Categories { get; set; }
}
and
[YAXSerializeAs("category")]
public class Category
{
[YAXSerializeAs("id")]
[YAXAttributeForClass]
public int Id { get; set; }
[YAXValueForClass]
public string CategoryName { get; set; }
/// <summary>
/// Assuming, this should also become an attribute to Category
/// (although DevOp's XML didn't contain it)
/// </summary>
[YAXSerializeAs("parentId")]
[YAXAttributeForClass]
[YAXErrorIfMissed(YAXExceptionTypes.Ignore)]
public int? ParentId { get; set; }
}

Error when trying to create an object from XML (C#)

I have an issue when trying to convert an xml string as an object, I keep getting "There is an error in XML document (1, 40)."
Here is my code :
httpResponse = await httpClient.GetAsync(requestUri);
httpResponse.EnsureSuccessStatusCode();
httpResponseBody = await httpResponse.Content.ReadAsStringAsync();
XmlSerializer serializer = new XmlSerializer(typeof(Feed));
StringReader rdr = new StringReader(httpResponseBody);
Feed resultingMessage = (Feed)serializer.Deserialize(rdr);
The class :
[XmlRoot("feed"), Serializable]
public class Feed
{
[XmlElement("title")]
public string title { get; set; }
[XmlElement("entry")]
public List<Entry> Entry { get; set; }
}
public class Entry
{
[XmlElement("content")]
public Content content { get; set; }
}
public class Content
{
[XmlElement("properties")]
public Properties properties { get; set; }
}
public class Properties
{
[XmlElement("EntityID")]
public int properties { get; set; }
[XmlElement("Latitude")]
public Double latitude { get; set; }
[XmlElement("Longitude")]
public Double longitude { get; set; }
[XmlElement("DisplayName")]
public Properties name { get; set; }
[XmlElement("__Distance")]
public Double distance { get; set; }
}
And an sample of the XML received :
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
<title type="text"></title>
<id>uuid:ce35d2a9-1966-4bd7-8730-80cff2a0ce58;id=13760</id>
<rights type="text">© 2017 Microsoft and its suppliers. This API and any results cannot be used or accessed without Microsoft's express written permission.</rights>
<updated>2017-06-28T19:27:04Z</updated>
<entry>
<id>https://spatial.virtualearth.net/REST/v1/data/c2ae584bbccc4916a0acf75d1e6947b4/NavteqEU/NavteqPOIs('800791175')</id>
<title type="text"></title>
<updated>2017-06-28T19:27:04Z</updated>
<content type="application/xml">
<m:properties>
<d:EntityID>800791175</d:EntityID>
<d:Latitude m:type="Edm.Double">47.386450</d:Latitude>
<d:Longitude m:type="Edm.Double">0.690600</d:Longitude>
<d:DisplayName>Au Chantecler</d:DisplayName>
<d:LanguageCode>FRE</d:LanguageCode>
<d:__Distance m:type="Edm.Double">0.0730920601952144</d:__Distance>
</m:properties>
</content>
</entry>
</feed>
When I remove the the error is not on 1,40 but 1,2.
Thanks in advance !
I resolved the issue, I forgot to specify the namespace for the elements, for instance in Feed:
[XmlElement(ElementName = "entry", Namespace = "http://www.w3.org/2005/Atom")]
public List<Entry> Entries { get; set; }

Deserialize with RestSharp

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?

c#: Deserializing xml with namespaces to clr object

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; }

XML Deserialization not deserializing element

Good Afternoon,
I have the following classes
public class MaintenanceBundle
{
[XmlAttribute(AttributeName = "Required")]
public string Required { get; set; }
[XmlAttribute(AttributeName = "ID")]
public string Id { get; set; }
[XmlElement(ElementName = "Title")]
public string Title { get; set; }
[XmlElement(ElementName = "MntReason")]
public MaintenanceReason Reason { get; set; }
[XmlElement(ElementName = "Tasks")]
public MaintenanceBundleCollection Tasks { get; set; }
}
public class MaintenanceBundleCollection
{
[XmlElement(ElementName = "Task")]
public List<MaintenanceBundleTask> Tasks { get; set; }
}
public class MaintenanceReason
{
[XmlAttribute(AttributeName = "Every")]
public string Every { get; set; }
[XmlElement(ElementName = "Mileage", IsNullable = true)]
public int? Mileage { get; set; }
[XmlElement(ElementName = "Time", IsNullable = true)]
public TimeInterval TimeInterval { get; set; }
}
I'm trying to deserialize this xml to objects using these classes. Here is the XML
<MntBundle Required="Yes" ID="S08870641702009101200000">
<Title>DIRT OR DUSTY ROADS - 5000 MILES / 6 MONTHS</Title>
<MntReason Every="No">
<Mileage Unit="MILES">5000</Mileage>
</MntReason>
<Tasks>
<Task ID="4-2" />
<Task ID="4-3">
<NMVCQualifier>Drive Shaft Boots</NMVCQualifier>
<MVCQualifiers>
<Qualifier Name="Drive Type">4WD</Qualifier>
</MVCQualifiers>
</Task>
<Task ID="4-1" />
<Task ID="4-4" />
<Task ID="5-1">
<MVCQualifiers>
<Qualifier Name="Drive Type">4WD</Qualifier>
</MVCQualifiers>
</Task>
<Task ID="6-1" />
<Task ID="7-1" />
</Tasks>
</MntBundle>
For some reason I am unable to get the Mileage element inside the MntReason element. It keeps coming back as null. Any ideas what I am doing wrong? All the other elements seem to deserialize properly. I left out irrelevant classes from my post. If anyone has any pointers how I can properly retreive this value I would love to hear it. Thanks so much for any help.
Cheers,
~ck in San Diego
Untested but it should need something like the class below. I'm not sure how XmlText will behave with an integer.
public class Mileage
{
[XmlAttribute(AttributeName = "Unit")]
public string Unit {get; set;}
[XmlText]
public int Mileage {get; set;}
}

Categories