Deserializing XML from url to Objects in C# - c#

So I have xml from a url that looks like this:
<restaurantFoodImpls>
<RestaurantFood Price="1689.7594" ID="426" Description="quis egreddior glavans brevens, si eggredior. vobis e fecundio, fecundio, et quoque nomen gravum parte volcans">
<foodItem Name="Frances93" ID="548"/>
<restaurant Name="Alana59" PhoneNumber="7954016342" MobileNumber="372206-3626" LastName="Hickman" ID="1" FirstName="Gabrielle"/>
</RestaurantFood>
<RestaurantFood Price="14.225095" ID="520" Description="in plorum egreddior plorum e pladior in linguens essit. novum habitatio Versus plurissimum volcans linguens estum.">
<foodItem Name="Frances93" ID="548"/>
<restaurant Name="Alana59" PhoneNumber="7954016342" MobileNumber="372206-3626" LastName="Hickman" ID="1" FirstName="Gabrielle"/>
</RestaurantFood>
</restaurantFoodImpls>
How to parse it into objects using C#?
I have tried using the deserializer but my problem is that I want the properties of the elements read from the attributes in XML, and I couldn't get them.

var stream = File.Open(filename, FileMode.Open);
XmlSerializer ser = new XmlSerializer(typeof(RestaurantFoodImpls));
var result = ser.Deserialize(stream) as RestaurantFoodImpls;
public class FoodItem
{
[XmlAttribute]
public string Name { get; set; }
[XmlAttribute]
public string ID { get; set; }
}
public class Restaurant
{
[XmlAttribute]
public string Name { get; set; }
[XmlAttribute]
public string PhoneNumber { get; set; }
[XmlAttribute]
public string MobileNumber { get; set; }
[XmlAttribute]
public string LastName { get; set; }
[XmlAttribute]
public string ID { get; set; }
[XmlAttribute]
public string FirstName { get; set; }
}
public class RestaurantFood
{
[XmlAttribute]
public string Price { get; set; }
[XmlAttribute]
public string ID { get; set; }
[XmlAttribute]
public string Description { get; set; }
[XmlElement("foodItem")]
public FoodItem foodItem { get; set; }
[XmlElement("restaurant")]
public Restaurant restaurant { get; set; }
}
[XmlRoot("restaurantFoodImpls")]
public class RestaurantFoodImpls
{
[XmlElement("RestaurantFood")]
public List<RestaurantFood> RestaurantFood { get; set; }
}

Related

How to map xml objects into list<elements> in c#?

I have a xml item tag which should be mapped into List. But while mapping apart from the list of item other elements has been mapped to the model.
I referred to other questions where each items is given a parent so they mapped while deserialising.
Let me know how to map to the given list item into the respective model.
XML:
<SecretModel>
<id>3181</id>
<name>Test</name>
<secretTemplateId>6044</secretTemplateId>
<folderId>674</folderId>
<active>true</active>
<items>
<itemId>13960</itemId>
<fileAttachmentId/>
<filename/>
<itemValue>Test</itemValue>
<fieldId>287</fieldId>
<fieldName>Username</fieldName>
<slug>username</slug>
<fieldDescription>The Amazon IAM username.</fieldDescription>
<isFile>false</isFile>
<isNotes>false</isNotes>
<isPassword>false</isPassword>
</items>
<items>
<itemId>13961</itemId>
<fileAttachmentId/>
<filename/>
<itemValue>AKIAU5G4MQBA2TKQOWNW</itemValue>
<fieldId>284</fieldId>
<fieldName>Access Key</fieldName>
<slug>access-key</slug>
<fieldDescription>The Amazon IAM access key.</fieldDescription>
<isFile>false</isFile>
<isNotes>false</isNotes>
<isPassword>false</isPassword>
</items>
</secretModel>
SecretModel.cs:
Public class SecretModel
{
public int? id {get; set;}
public string name {get; set;}
public string SecretTemplateID {get; set;}
public string folderId {get; set;}
public string active {get; set;}
public string List<RestSecretItem> {get; set;}
}
public partial class RestSecretItem : IEquatable<RestSecretItem>
{
public string FieldDescription { get; set; }
public int? FieldId { get; set; }
public string FieldName { get; set; }
public int? FileAttachmentId { get; set; }
public string Filename { get; set; }
public bool? IsFile { get; set; }
public bool? IsNotes { get; set; }
public bool? IsPassword { get; set; }
public int? ItemId { get; set; }
public string ItemValue { get; set; }
public string Slug { get; set; }
}
There are many ways to map xml, one is using built-in .NET XML serializer. Here is one way to serialize it.
SecretModel
[XmlRoot(ElementName = "SecretModel")]
public class SecretModel
{
[XmlAttribute(AttributeName = "id")]
public int Id { get; set; }
[XmlAttribute(AttributeName = "name")]
public string Name { get; set; }
[XmlAttribute(AttributeName = "secretTemplateId")]
public string SecretTemplateId { get; set; }
[XmlAttribute(AttributeName = "folderId")]
public string FolderId { get; set; }
[XmlAttribute(AttributeName = "active")]
public bool Active { get; set; }
[XmlElement(ElementName = "items")]
public List<RestSecretItem> Items { get; set; }
}
RestSecretItem
[XmlRoot(ElementName = "items")]
public class RestSecretItem
{
[XmlAttribute(AttributeName = "itemId")]
public int ItemId { get; set; }
[XmlAttribute(AttributeName = "fileAttachmentId")]
public int FileAttachmentId { get; set; }
[XmlAttribute(AttributeName = "filename")]
public string FileName { get; set; }
[XmlAttribute(AttributeName = "itemValue")]
public string ItemValue { get; set; }
[XmlAttribute(AttributeName = "fieldId")]
public int FieldId { get; set; }
[XmlAttribute(AttributeName = "fieldName")]
public string FieldName { get; set; }
[XmlAttribute(AttributeName = "slug")]
public string Slug { get; set; }
[XmlAttribute(AttributeName = "fieldDescription")]
public string FieldDescription { get; set; }
[XmlAttribute(AttributeName = "isFile")]
public bool IsFile { get; set; }
[XmlAttribute(AttributeName = "isNotes")]
public bool IsNote { get; set; }
[XmlAttribute(AttributeName = "isPassword")]
public bool IsPassword { get; set; }
}
Now you can parse the xml through using this code:
var doc = new XmlDocument();
doc.load("your directory or path of file here");
using(var reader = new StringReader(doc.InnerXml))
{
var serializer = new XmlSerializer(typeof(SecretModel));
var data = (SecretModel)serializer.Deserialize(reader); // converted model here
}

How to Serialize to a "Collection with an Attribute" Using the XML Serializer

I have this model:
[XmlArray(ElementName = "Listing")]
[XmlArrayItem(ElementName = "Classification")]
public List<Classification> classifications { get; set; }
[XmlRoot("Listing")]
public class Classification
{
[XmlAttribute("Name")]
public string name { get; set; }
[XmlText]
public string Value { get; set; }
}
That gives me this:
<Listing>
<Classification Name="Location">AsiaPacific</Classification>
</Listing>
How should I modify my class to get this:
<Listing reference = "MyReference">
<Classification Name="Location">AsiaPacific</Classification>
</Listing>
After a few (hundreds) trial and error, I got the result that I need by modifying the model to:
[XmlElement(ElementName = "Listing")]
public ClassificationWrapper classificationWrapper { get; set; }
public class ClassificationWrapper
{
[XmlAttribute("reference")]
public string ref= "MyReference";
[XmlElement("Classification", typeof(Classification))]
public List<Classification> classifications { get; set; }
public ClassificationWrapper() { this.classifications = new List<Classification>(); }
}
public class Classification
{
[XmlAttribute("Name")]
public string name { get; set; }
[XmlText]
public string Value { get; set; }
}

JSONConvert.DeserializeObject not handling child array with unnamed array items

I have the following JSON object coming to me from a web service
{
"room":{
"name":"Thunderdome",
"created_at":"2012/04/15 00:36:27 +0000",
"id":xxxxxxx,
"users":[
{
"type":"Member",
"avatar_url":"url",
"created_at":"2012/02/27 14:11:57 +0000",
"id":1139474,
"email_address":"xxx#xxxxxxx.com",
"admin":false,
"name":"xxxx xxxxx"
},
{
"type":"Member",
etc
I'm using the following line to deserialize:
var room = JsonConvert.DeserializeObject<SingleRoom>(text);
And the following mapping classes
public class SingleRoom
{
public Room Room { get; set; }
}
[DataContract]
public class Room
{
[DataMember]
public int Id { get; set; }
[DataMember]
public string Name { get; set; }
public string Image {
get { return "Assets/campfire.png"; }
}
[DataMember]
public string Topic { get; set; }
[DataMember(Name ="membership_limit")]
public int MembershipLimit { get; set; }
[DataMember]
public bool Full { get; set; }
[DataMember]
public bool Locked { get; set; }
[DataMember(Name = "open_to_guests")]
public bool OpenToGuests { get; set; }
[DataMember(Name = "updated_at")]
public DateTime UpdatedAt { get; set; }
[DataMember(Name = "created_at")]
public DateTime CreatedAt { get; set; }
[DataMember(Name = "active_token_value")]
public string ActiveTokenValue { get; set; }
[DataMember(Name = "Users")]
public List<User> Users { get; set; }
}
[DataContract]
public class User
{
[DataMember]
public int Id { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember(Name = "email_address")]
public string EmailAddress { get; set; }
[DataMember]
public bool Admin { get; set; }
[DataMember]
public DateTime CreatedAt { get; set; }
[DataMember]
public string Type { get; set; }
[DataMember(Name = "avatar_url")]
public string AvatarUrl { get; set; }
}
The 'Room' object deserializes correctly but the Users property on the Room is always null. I know that a null result is the result of a JSON.NET serialization that couldn't find the matching property. However, I think I have it right? List should match to user. I know the array object users in the JSON doesn't have named children, is that the issue? If so, how do I solve it?
Thanks!
This works.... You can rename them (or use JsonProperty attribute) to use c# style property names
(BTW: Json.Net doesn't require DataMember, DataContract attributes)
var obj = JsonConvert.DeserializeObject<SingleRoom>(json)
public class User
{
public string type { get; set; }
public string avatar_url { get; set; }
public string email_address { get; set; }
public bool admin { get; set; }
public string name { get; set; }
public string created_at { get; set; }
public int id { get; set; }
}
public class Room
{
public string topic { get; set; }
public int membership_limit { get; set; }
public bool locked { get; set; }
public string name { get; set; }
public List<User> users { get; set; }
public bool full { get; set; }
public bool open_to_guests { get; set; }
public string updated_at { get; set; }
public string created_at { get; set; }
public int id { get; set; }
}
public class SingleRoom
{
public Room room { get; set; }
}
PS: You may find that site useful http://json2csharp.com/ .
In my case, I had missing to add the "public" key for the child property.

Can't Deserialize XML

I am trying to deserialize the following XML:
<?xml version="1.0" encoding="UTF-8"?>
<jobInfo
xmlns="http://www.force.com/2009/06/asyncapi/dataload">
<id>750x0000000005LAAQ</id>
<operation>insert</operation>
<object>Contact</object>
<createdById>005x0000000wPWdAAM</createdById>
<createdDate>2009-09-01T16:42:46.000Z</createdDate>
<systemModstamp>2009-09-01T16:42:46.000Z</systemModstamp>
<state>Open</state>
<concurrencyMode>Parallel</concurrencyMode>
<contentType>CSV</contentType>
<numberBatchesQueued>0</numberBatchesQueued>
<numberBatchesInProgress>0</numberBatchesInProgress>
<numberBatchesCompleted>0</numberBatchesCompleted>
<numberBatchesFailed>0</numberBatchesFailed>
<numberBatchesTotal>0</numberBatchesTotal>
<numberRecordsProcessed>0</numberRecordsProcessed>
<numberRetries>0</numberRetries>
<apiVersion>28.0</apiVersion>
</jobInfo>
I have created this object:
public class JobInfo
{
public string xmlns { get; set; }
public string id { get; set; }
public string operation { get; set; }
public string #object { get; set; }
public string createdById { get; set; }
public string createdDate { get; set; }
public string systemModstamp { get; set; }
public string state { get; set; }
public string concurrencyMode { get; set; }
public string contentType { get; set; }
public string numberBatchesQueued { get; set; }
public string numberBatchesInProgress { get; set; }
public string numberBatchesCompleted { get; set; }
public string numberBatchesFailed { get; set; }
public string numberBatchesTotal { get; set; }
public string numberRecordsProcessed { get; set; }
public string numberRetries { get; set; }
public string apiVersion { get; set; }
}
public class RootObject
{
public JobInfo jobInfo { get; set; }
}
And, I am using this code:
XmlSerializer serializer = new XmlSerializer(typeof(RootObject));
StringReader rdr = new StringReader(response);
RootObject resultingMessage = (RootObject)serializer.Deserialize(rdr);
However, I get this error:
There is an error in XML document (1, 40).
{"<jobInfo xmlns='http://www.force.com/2009/06/asyncapi/dataload'> was not expected."}
How can I account for the xmlns attribute? My code has it as a property (thus, it fails)...
Currently your code is expecting the xmlns as element, not the attribute.
You should add the XmlSerialization attributes to your class, like this:
[XmlRoot("jobInfo", Namespace="http://www.force.com/2009/06/asyncapi/dataload"]
public class JobInfo
{
[XmlAttribute]
public string xmlns { get; set; }
[XmlElement(ElementName = "id")]
public string id { get; set; }
....
}
You can find more information in MSDN articles:
Controlling XML Serialization Using Attributes
Examples of XML Serialization
Try using the XmlRootAttribute to specify the Namespace.
[XmlRoot("jobInfo", Namespace = "http://www.force.com/2009/06/asyncapi/dataload")]
public class JobInfo
{
[XmlElement("id")]
public string Id { get; set; }
[XmlElement("operation")]
public string Operation { get; set; }
[XmlElement("object")]
public string Object { get; set; }
[XmlElement("createdById")]
public string CreatedById { get; set; }
[XmlElement("createdDate")]
public DateTime CreatedDate { get; set; }
[XmlElement("systemModstamp")]
public DateTime SystemModstamp { get; set; }
[XmlElement("state")]
public string State { get; set; }
[XmlElement("concurrencyMode")]
public string ConcurrencyMode { get; set; }
[XmlElement("contentType")]
public string ContentType { get; set; }
[XmlElement("numberBatchesQueued")]
public string NumberBatchesQueued { get; set; }
[XmlElement("numberBatchesInProgress")]
public string NumberBatchesInProgress { get; set; }
[XmlElement("numberBatchesCompleted")]
public string NumberBatchesCompleted { get; set; }
[XmlElement("numberBatchesFailed")]
public string NumberBatchesFailed { get; set; }
[XmlElement("numberBatchesTotal")]
public string NumberBatchesTotal { get; set; }
[XmlElement("numberRecordsProcessed")]
public string numberRecordsProcessed { get; set; }
[XmlElement("numberRetries")]
public string NumberRetries { get; set; }
[XmlElement("apiVersion")]
public string ApiVersion { get; set; }
}
Code:
var xml = #"<?xml version=""1.0"" encoding=""UTF-8""?>
<jobInfo xmlns=""http://www.force.com/2009/06/asyncapi/dataload"">
<id>750x0000000005LAAQ</id>
<operation>insert</operation>
<object>Contact</object>
<createdById>005x0000000wPWdAAM</createdById>
<createdDate>2009-09-01T16:42:46.000Z</createdDate>
<systemModstamp>2009-09-01T16:42:46.000Z</systemModstamp>
<state>Open</state>
<concurrencyMode>Parallel</concurrencyMode>
<contentType>CSV</contentType>
<numberBatchesQueued>0</numberBatchesQueued>
<numberBatchesInProgress>0</numberBatchesInProgress>
<numberBatchesCompleted>0</numberBatchesCompleted>
<numberBatchesFailed>0</numberBatchesFailed>
<numberBatchesTotal>0</numberBatchesTotal>
<numberRecordsProcessed>0</numberRecordsProcessed>
<numberRetries>0</numberRetries>
<apiVersion>28.0</apiVersion>
</jobInfo>";
var serializer = new XmlSerializer(typeof(JobInfo));
JobInfo jobInfo;
using(var stream = new StringReader(xml))
using(var reader = XmlReader.Create(stream))
{
jobInfo = (JobInfo)serializer.Deserialize(reader);
}
Add the namespace
[XmlRoot("jobInfo",Namespace = "http://www.force.com/2009/06/asyncapi/dataload")]
public class JobInfo
{
// get rid of the xmlns property
// other properties stay
}
// there is no rootobject, JobInfo is your root
XmlSerializer serializer = new XmlSerializer(typeof(JobInfo));
StringReader rdr = new StringReader(response);
JobInfo resultingMessage = (JobInfo)serializer.Deserialize(rdr);

How to Deserialize an JSON object with invalid field name in it

I have a JSON request which has follwing structure:
"formats": {
"flash_embed": "http://a3.vikiassets.com/assets/vikiplayer-922746a667cfd38137a7e45df6ba1b95.swf?auto_play=true&language_codes=en&media_id=74965&partner=16&source=api_v3",
"m3u8": "http://wpc.354a.edgecastcdn.net/00354A/videos/encoded/74965/ios.m3u8",
"res-150p": "http://wpc.354a.edgecastcdn.net/00354A/videos/encoded/74965_mp4cell_150.mp4",
"res-240p": "http://wpc.354a.edgecastcdn.net/00354A/videos/encoded/74965_240p.mp4",
"res-270p": "http://wpc.354a.edgecastcdn.net/00354A/videos/encoded/74965_270p.mp4",
"res-360p": "http://wpc.354a.edgecastcdn.net/00354A/videos/encoded/74965_360p.mp4",
"res-480p": "http://wpc.354a.edgecastcdn.net/00354A/videos/encoded/74965_480p.mp4",
"res-720p": "http://wpc.354a.edgecastcdn.net/00354A/videos/encoded/74965_720p.mp4"
}
}
Now res-150p, says invalid name in C# and if I give another name to it then while desiralizing I am not getting any values, that is null inside res-150p.
Edit:
[Serializable]
MoviesListRootObject is the root Object which Contains Response and then Response Contains Formats
public class MoviesListRootObject
{
public int count { get; set; }
public Pagination pagination { get; set; }
public List<Response> response { get; set; }
}
[Serializable]
public class Response
{
public int id { get; set; }
public int channel_id { get; set; }
public string title { get; set; }
public string title_language { get; set; }
public string description { get; set; }
public string description_language { get; set; }
public string created_at { get; set; }
public string uri { get; set; }
public string web_uri { get; set; }
public List<object> genres { get; set; }
public string origin_country { get; set; }
public string image { get; set; }
public Subtitles subtitles { get; set; }
public Formats formats { get; set; }
}
[Serializable]
public class Formats
{
public string flash_embed { get; set; }
public string m3u8 { get; set; }
public string __invalid_name__res150p { get; set; }
public string __invalid_name__res240p { get; set; }
public string __invalid_name__res270p { get; set; }
public string __invalid_name__res360p { get; set; }
public string __invalid_name__res480p { get; set; }
public string __invalid_name__res720p { get; set; }
public string __invalid_name__flv480p { get; set; }
public string __invalid_name__flv360p { get; set; }
public string __invalid_name__flv270p { get; set; }
public string __invalid_name__flvvp6360p { get; set; }
public string __invalid_name__flvvp6270p { get; set; }
}
You have to decorate your Formats properties with JsonProperty attributes to tell it what goes where if the name does not exactly match:
partial class Formats
{
[JsonProperty("res-150p")]
public string __invalid_name__res150p {get; set;}
}
See also Using JsonConvert.DeserializeObject to deserialize Json to a C# POCO class

Categories