C# InnerXml of XmlElement deserialization - c#

I have inner XML that looks like:
<approving xmlns="uz:rwc:fdu-92:1.0">
<fdu92>
<general num="25120001" date="25.12.2013" payment_kind="tehpd" sum="22200" />
<station code="370006" name="Test" road_short_name="Test" />
<cargo-owner code="7765" name="Test Subject" />
<payer code="7395392" name="Test Subject" />
<references>
<reference num="11111" doc_name="soma document">
<payments>
<payment code="158" subcode="001" name="Some work" reason="unknown" sum="22200" expended_count="1" />
</payments>
</reference>
</references>
</fdu92>
</approving>
<signature xmlns="uz:rwc:fdu-92:1.0">Many thousands of symbols</signature>
When I am trying to build classes from Xml by online tool xml to classes
it return Invalid Xml.
Because it has
<signature xmlns="uz:rwc:fdu-92:1.0">Many thousands of symbols</signature>
Converting without tag
<signature xmlns="uz:rwc:fdu-92:1.0">Many thousands of symbols</signature>
gives good result
namespace Xml2CSharp
{
[XmlRoot(ElementName="general", Namespace="uz:rwc:fdu-92:1.0")]
public class General {
[XmlAttribute(AttributeName="num")]
public string Num { get; set; }
[XmlAttribute(AttributeName="date")]
public string Date { get; set; }
[XmlAttribute(AttributeName="payment_kind")]
public string Payment_kind { get; set; }
[XmlAttribute(AttributeName="sum")]
public string Sum { get; set; }
}
[XmlRoot(ElementName="station", Namespace="uz:rwc:fdu-92:1.0")]
public class Station {
[XmlAttribute(AttributeName="code")]
public string Code { get; set; }
[XmlAttribute(AttributeName="name")]
public string Name { get; set; }
[XmlAttribute(AttributeName="road_short_name")]
public string Road_short_name { get; set; }
}
[XmlRoot(ElementName="cargo-owner", Namespace="uz:rwc:fdu-92:1.0")]
public class Cargoowner {
[XmlAttribute(AttributeName="code")]
public string Code { get; set; }
[XmlAttribute(AttributeName="name")]
public string Name { get; set; }
}
[XmlRoot(ElementName="payer", Namespace="uz:rwc:fdu-92:1.0")]
public class Payer {
[XmlAttribute(AttributeName="code")]
public string Code { get; set; }
[XmlAttribute(AttributeName="name")]
public string Name { get; set; }
}
[XmlRoot(ElementName="payment", Namespace="uz:rwc:fdu-92:1.0")]
public class Payment {
[XmlAttribute(AttributeName="code")]
public string Code { get; set; }
[XmlAttribute(AttributeName="subcode")]
public string Subcode { get; set; }
[XmlAttribute(AttributeName="name")]
public string Name { get; set; }
[XmlAttribute(AttributeName="reason")]
public string Reason { get; set; }
[XmlAttribute(AttributeName="sum")]
public string Sum { get; set; }
[XmlAttribute(AttributeName="expended_count")]
public string Expended_count { get; set; }
}
[XmlRoot(ElementName="payments", Namespace="uz:rwc:fdu-92:1.0")]
public class Payments {
[XmlElement(ElementName="payment", Namespace="uz:rwc:fdu-92:1.0")]
public Payment Payment { get; set; }
}
[XmlRoot(ElementName="reference", Namespace="uz:rwc:fdu-92:1.0")]
public class Reference {
[XmlElement(ElementName="payments", Namespace="uz:rwc:fdu-92:1.0")]
public Payments Payments { get; set; }
[XmlAttribute(AttributeName="num")]
public string Num { get; set; }
[XmlAttribute(AttributeName="doc_name")]
public string Doc_name { get; set; }
}
[XmlRoot(ElementName="references", Namespace="uz:rwc:fdu-92:1.0")]
public class References {
[XmlElement(ElementName="reference", Namespace="uz:rwc:fdu-92:1.0")]
public Reference Reference { get; set; }
}
[XmlRoot(ElementName="fdu92", Namespace="uz:rwc:fdu-92:1.0")]
public class Fdu92 {
[XmlElement(ElementName="general", Namespace="uz:rwc:fdu-92:1.0")]
public General General { get; set; }
[XmlElement(ElementName="station", Namespace="uz:rwc:fdu-92:1.0")]
public Station Station { get; set; }
[XmlElement(ElementName="cargo-owner", Namespace="uz:rwc:fdu-92:1.0")]
public Cargoowner Cargoowner { get; set; }
[XmlElement(ElementName="payer", Namespace="uz:rwc:fdu-92:1.0")]
public Payer Payer { get; set; }
[XmlElement(ElementName="references", Namespace="uz:rwc:fdu-92:1.0")]
public References References { get; set; }
}
[XmlRoot(ElementName="approving", Namespace="uz:rwc:fdu-92:1.0")]
public class Approving {
[XmlElement(ElementName="fdu92", Namespace="uz:rwc:fdu-92:1.0")]
public Fdu92 Fdu92 { get; set; }
[XmlAttribute(AttributeName="xmlns")]
public string Xmlns { get; set; }
}
}
When I am trying to deserialize the object
DeserializeFromXmlElement<Approving>(FDU92Xml);
public static T DeserializeFromXmlElement<T>(XmlElement element)
{
var serializer = new XmlSerializer(typeof(T));
return (T)serializer.Deserialize(new XmlNodeReader(element));
}
I get an error
xml document contains error
How to fix error? How to ignore signature in deserialization process?

Related

github api get commits and commiters

I am trying to get the list of commiters
In beginning I ask GitHub API (v3) to get repositories:
https://api.github.com/users/{userName}/repos
ok, got some list, and I got there repository name, which I want to use to get all commits
https://api.github.com/repos/{userName}/{repoName}/commits
the address is correct, I get some information, but...
I don't know how to create classes, I don't understand this JSON structure
see the example on https://api.github.com/repos/octocat/Hello-World/git/commits/
its something like this:
[0]
{
"sha",
"commit":{
"commiter":{
"name",
"email",
}
}
}
so... the commit class should have commit...?
I have tried something like this:
the Commit class
[DataContract(Name="commits")]
public class Commit
{
[DataMember(Name="sha")]
public string Sha{get;set;}
[DataMember(Name="commiter")]
public Commiter Commiter{get;set;}
}
and the commiter class:
[DataContract(Name="commiter")]
public class Commiter
{
[DataMember(Name="name")]
public string Name{get;set;}
[DataMember(Name="email")]
public string Email {get;set;}
[DataMember(Name="date")]
public string Date{get;set;}
[DataMember(Name="message")]
public string Message{get;set;}
}
but the only result when I ask for commits is the SHA field from Commit class, the committer is always null
can someone help me :)?
For generate my POCO class I always use :
http://json2csharp.com/
It's automatic.
For your API it's :
public class Author
{
public string name { get; set; }
public string email { get; set; }
public DateTime date { get; set; }
}
public class Committer
{
public string name { get; set; }
public string email { get; set; }
public DateTime date { get; set; }
}
public class Tree
{
public string sha { get; set; }
public string url { get; set; }
}
public class Verification
{
public bool verified { get; set; }
public string reason { get; set; }
public string signature { get; set; }
public string payload { get; set; }
}
public class Commit
{
public Author author { get; set; }
public Committer committer { get; set; }
public string message { get; set; }
public Tree tree { get; set; }
public string url { get; set; }
public int comment_count { get; set; }
public Verification verification { get; set; }
}
public class Author2
{
public string login { get; set; }
public int id { get; set; }
public string node_id { get; set; }
public string avatar_url { get; set; }
public string gravatar_id { get; set; }
public string url { get; set; }
public string html_url { get; set; }
public string followers_url { get; set; }
public string following_url { get; set; }
public string gists_url { get; set; }
public string starred_url { get; set; }
public string subscriptions_url { get; set; }
public string organizations_url { get; set; }
public string repos_url { get; set; }
public string events_url { get; set; }
public string received_events_url { get; set; }
public string type { get; set; }
public bool site_admin { get; set; }
}
public class Committer2
{
public string login { get; set; }
public int id { get; set; }
public string node_id { get; set; }
public string avatar_url { get; set; }
public string gravatar_id { get; set; }
public string url { get; set; }
public string html_url { get; set; }
public string followers_url { get; set; }
public string following_url { get; set; }
public string gists_url { get; set; }
public string starred_url { get; set; }
public string subscriptions_url { get; set; }
public string organizations_url { get; set; }
public string repos_url { get; set; }
public string events_url { get; set; }
public string received_events_url { get; set; }
public string type { get; set; }
public bool site_admin { get; set; }
}
public class Parent
{
public string sha { get; set; }
public string url { get; set; }
public string html_url { get; set; }
}
public class RootObject
{
public string sha { get; set; }
public string node_id { get; set; }
public Commit commit { get; set; }
public string url { get; set; }
public string html_url { get; set; }
public string comments_url { get; set; }
public Author2 author { get; set; }
public Committer2 committer { get; set; }
public List<Parent> parents { get; set; }
}

Noob C# Class Declaration Issue

So I created a class using json2csharp
public class ResponseType
{
public class Query
{
public string q { get; set; }
public object sku { get; set; }
public int limit { get; set; }
public object reference { get; set; }
public object mpn_or_sku { get; set; }
public string mpn { get; set; }
public object brand { get; set; }
public string __class__ { get; set; }
public int start { get; set; }
public object seller { get; set; }
}
public class Request
{
public bool exact_only { get; set; }
public string __class__ { get; set; }
public List<Query> queries { get; set; }
}
public class Seller
{
public string display_flag { get; set; }
public bool has_ecommerce { get; set; }
public string name { get; set; }
public string __class__ { get; set; }
public string homepage_url { get; set; }
public string id { get; set; }
public string uid { get; set; }
}
public class Prices
{
public List<List<object>> USD { get; set; }
public List<List<object>> JPY { get; set; }
public List<List<object>> CNY { get; set; }
}
public class Offer
{
public string sku { get; set; }
public string packaging { get; set; }
public string on_order_eta { get; set; }
public string last_updated { get; set; }
public int? order_multiple { get; set; }
public int in_stock_quantity { get; set; }
public string eligible_region { get; set; }
public int? moq { get; set; }
public int? on_order_quantity { get; set; }
public object octopart_rfq_url { get; set; }
public string __class__ { get; set; }
public Seller seller { get; set; }
public string product_url { get; set; }
public object factory_order_multiple { get; set; }
public string _naive_id { get; set; }
public int? factory_lead_days { get; set; }
public Prices prices { get; set; }
public bool is_authorized { get; set; }
public bool is_realtime { get; set; }
}
public class Brand
{
public string homepage_url { get; set; }
public string __class__ { get; set; }
public string name { get; set; }
public string uid { get; set; }
}
public class Manufacturer
{
public string homepage_url { get; set; }
public string __class__ { get; set; }
public string name { get; set; }
public string uid { get; set; }
}
public class Item
{
public List<Offer> offers { get; set; }
public string uid { get; set; }
public string mpn { get; set; }
public List<object> redirected_uids { get; set; }
public Brand brand { get; set; }
public string octopart_url { get; set; }
public string __class__ { get; set; }
public Manufacturer manufacturer { get; set; }
}
public class Result
{
public List<Item> items { get; set; }
public int hits { get; set; }
public string __class__ { get; set; }
public object reference { get; set; }
public object error { get; set; }
}
public class RootObject
{
public int msec { get; set; }
public Request request { get; set; }
public string __class__ { get; set; }
public List<Result> results { get; set; }
}
}
The problem is at design-time, when I declare a variable with the type of my class:
ResponseType Response = new ResponseType();
Intellisense does not allow me to access the subclasses RootObject.results list. It only shows Equals, GetHashCode, GetType and ToString. I am assuming I did something wrong in my class declaration.
Thank you in advance!
Edit -- I am fairly new to C Sharp. I am trying to parse a response from a REST API. I took the JSON provided by the Rest API and converted it using json2csharp into a class. My intent was to do something like this
Within a function return:
public ResponseType ExecuteSearch(String PartNumber)
{
~ ALL CODE FOR GENERATING req
// Perform the search and obtain results
var data = client.Execute(req).Content;
JSON = data;
return JsonConvert.DeserializeObject<ResponseType>(data);
}
Then being able to access the response as an object outside of the function
Edit 2:
I figured out what I did. Instead of nesting everything within the ResponseType I should have simply renamed RootObject to ResponseType.
Intellisense does not allow me to access the subclasses RootObject.results list
it is because the property results is not static and you try to acces it this way. A static property is accessed via ClassName.PropertyName. For more information on static variables check the link.
It only shows Equals, GetHashCode, GetType and ToString
This is the basic set of methods that every object in C# inherits from the class object. This is why you can see it.
Intellisense will allow you to do this:
ResponseType.RootObject ro = new ResponseType.RootObject();
ro.results.First();
because you will need an Instance of that class to acces the property results.
I am assuming I did something wrong in my class declaration.
It depends. Basically if the compiler does not complain then you declared your classes as supposed to be. But the declaration of the properties commands you to access them in a specific way. So if you still want to access results with RootObject.results you need to make it static:
public class RootObject
{
public static List<Result> results { get; set; }
}
But note that this list will exist only once! and is not individual to each instance of RootObject! Since you have embedded classes you need to call it like this:
ResponseType.RootObject.results.WhatEver();
EDIT
I guess you would like to get the Object of type RootObject inside the Object of type ResponseType. If I am right then it is not necessary to declare the classes inside ResponseType but you have to declare variables of each type inside it like:
public class ResponseType
{
public RootObject MyRootObject{ get; set; }
}
public class RootObject
{
public int msec { get; set; }
public Request request { get; set; }
public string __class__ { get; set; }
public List<Result> results { get; set; }
}
Now you will be able to access the results variable inside the ResponseType object:
ResponseType rt = new ResponseType();
rt.MyRootObject.results.WhatEver();
For more information on how to deserialize JSON to classes please read the Deserialize JSON to C# Classes post
1) Object with ResponseType class isn't contain any fields(event static one).
2) You declare ResponseType object, but results is field of RootObject object.
So if you want to work with results you should do something like this:
ResponseType.RootObject rootObject = new ResponseType.RootObject();
rootObject.results.DoWork();
Below is what I think you are trying to do. I would only use it in this form if this is some kind of Data Transfer Object (DTO) because otherwise it is pretty bad practice for a class that would be used in code (mostly because of the public getters and setters on all of the fields and the field names matching the class name), but it does show your main mistake and that is that classes need to be defined outside of your main class and if you need that type of class in your top level class you need to define a public field to access it.
public class ResponseType
{
public Query Query { get; set; }
public Request Request { get; set; }
public Seller Seller { get; set; }
public Prices Prices { get; set; }
public Offer Offer { get; set; }
public Brand Brand { get; set; }
public Manufacturer Manufacturer { get; set; }
public Item Item { get; set; }
public Result Result { get; set; }
public RootObject RootObject { get; set; }
}
public class Query
{
public string q { get; set; }
public object sku { get; set; }
public int limit { get; set; }
public object reference { get; set; }
public object mpn_or_sku { get; set; }
public string mpn { get; set; }
public object brand { get; set; }
public string __class__ { get; set; }
public int start { get; set; }
public object seller { get; set; }
}
public class Request
{
public bool exact_only { get; set; }
public string __class__ { get; set; }
public List<Query> queries { get; set; }
}
public class Seller
{
public string display_flag { get; set; }
public bool has_ecommerce { get; set; }
public string name { get; set; }
public string __class__ { get; set; }
public string homepage_url { get; set; }
public string id { get; set; }
public string uid { get; set; }
}
public class Prices
{
public List<List<object>> USD { get; set; }
public List<List<object>> JPY { get; set; }
public List<List<object>> CNY { get; set; }
}
public class Offer
{
public string sku { get; set; }
public string packaging { get; set; }
public string on_order_eta { get; set; }
public string last_updated { get; set; }
public int? order_multiple { get; set; }
public int in_stock_quantity { get; set; }
public string eligible_region { get; set; }
public int? moq { get; set; }
public int? on_order_quantity { get; set; }
public object octopart_rfq_url { get; set; }
public string __class__ { get; set; }
public Seller seller { get; set; }
public string product_url { get; set; }
public object factory_order_multiple { get; set; }
public string _naive_id { get; set; }
public int? factory_lead_days { get; set; }
public Prices prices { get; set; }
public bool is_authorized { get; set; }
public bool is_realtime { get; set; }
}
public class Brand
{
public string homepage_url { get; set; }
public string __class__ { get; set; }
public string name { get; set; }
public string uid { get; set; }
}
public class Manufacturer
{
public string homepage_url { get; set; }
public string __class__ { get; set; }
public string name { get; set; }
public string uid { get; set; }
}
public class Item
{
public List<Offer> offers { get; set; }
public string uid { get; set; }
public string mpn { get; set; }
public List<object> redirected_uids { get; set; }
public Brand brand { get; set; }
public string octopart_url { get; set; }
public string __class__ { get; set; }
public Manufacturer manufacturer { get; set; }
}
public class Result
{
public List<Item> items { get; set; }
public int hits { get; set; }
public string __class__ { get; set; }
public object reference { get; set; }
public object error { get; set; }
}
public class RootObject
{
public int msec { get; set; }
public Request request { get; set; }
public string __class__ { get; set; }
public List<Result> results { get; set; }
}

Deserialize XML elements

I have some problems deserializing my XML into class.
This is the XML:
<?xml version="1.0" encoding="utf-8" ?>
<InterestRates>
<!--Type A -->
<InterestRates_A>
<InterestRate_A>
<ValidFrom></ValidFrom>
<ValidTo>2004-12-31</ValidTo>
<Rate>0.00000</Rate>
</InterestRate_A>
<InterestRate_A>
<ValidFrom>2005-01-01</ValidFrom>
<ValidTo>2005-12-31</ValidTo>
<Rate>0.04247</Rate>
</InterestRate_A>
<InterestRate_A>
<ValidFrom>2005-01-01</ValidFrom>
<ValidTo>2005-12-31</ValidTo>
<Rate>0.04247</Rate>
</InterestRate_A>
<InterestRate_A>
<ValidFrom>2006-01-01</ValidFrom>
<ValidTo>2006-12-31</ValidTo>
<Rate>0.02986</Rate>
</InterestRate_A>
<InterestRate_A>
<ValidFrom>2007-01-01</ValidFrom>
<ValidTo>2009-10-30</ValidTo>
<Rate>0.02740</Rate>
</InterestRate_A>
<InterestRate_A>
<ValidFrom>2009-10-31</ValidFrom>
<ValidTo>2009-10-30</ValidTo>
<Rate>0.02470</Rate>
</InterestRate_A>
</InterestRates_A>
<!--Type B -->
<InterestRates_B>
<InterestRate_B>
<ValidFrom>2016-05-01</ValidFrom>
<ValidTo></ValidTo>
<Rate>0.05</Rate>
<Rate2>2.05</Rate2>
</InterestRate_B>
</InterestRates_B>
<!--Type C -->
<InterestRates_C>
<InterestRate_C>
<ValidFrom>2017-01-01</ValidFrom>
<ValidTo></ValidTo>
<Rate>2</Rate>
</InterestRate_C>
</InterestRates_C>
<!--Type D -->
<InterestRates_D>
<InterestRate_D>
<ValidFrom>2017-01-01</ValidFrom>
<ValidTo></ValidTo>
<Rate>3</Rate>
</InterestRate_D>
</InterestRates_D>
<!--Type E -->
<InterestRates_E>
<InterestRate_E>
<ValidFrom>2017-01-01</ValidFrom>
<ValidTo></ValidTo>
<Rate>5</Rate>
</InterestRate_E>
</InterestRates_E>
<!--Type F -->
<InterestRates_F>
<InterestRate_F>
<ValidFrom>2017-01-01</ValidFrom>
<ValidTo></ValidTo>
<Rate>7</Rate>
</InterestRate_F>
</InterestRates_F>
</InterestRates>
This is the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
namespace Interest
{
// Root class
[XmlRoot("InterestRates")]
public class InterestRates
{
[XmlElement("InterestRates_A")]
public InterestRates_A InterestRates_A { get; set; }
[XmlElement("InterestRates_B")]
public InterestRates_B InterestRates_B { get; set; }
[XmlElement("InterestRates_C")]
public InterestRates_C InterestRates_C { get; set; }
[XmlElement("InterestRates_D")]
public InterestRates_D InterestRates_D { get; set; }
[XmlElement("InterestRates_E")]
public InterestRates_E InterestRates_E { get; set; }
[XmlElement("InterestRates_F")]
public InterestRates_F InterestRates_F { get; set; }
}
[XmlType("InterestRates_A")]
public class InterestRates_A
{
[XmlArrayItem("InterestRate_A", typeof(InterestRate_A))]
public InterestRate_A[] InterestRate_A { get; set; }
}
[XmlType("InterestRates_B")]
public class InterestRates_B
{
[XmlArray("InterestRate_B")]
[XmlArrayItem("InterestRate_B", typeof(InterestRate_B))]
public InterestRate_B[] InterestRate_B { get; set; }
}
[XmlType("InterestRates_C")]
public class InterestRates_C
{
[XmlArray("InterestRate_C")]
[XmlArrayItem("InterestRate_C", typeof(InterestRate_C))]
public InterestRate_C[] InterestRate_C { get; set; }
}
[XmlType("InterestRates_D")]
public class InterestRates_D
{
[XmlArray("InterestRate_D")]
[XmlArrayItem("InterestRate_D", typeof(InterestRate_D))]
public InterestRate_D[] InterestRate_D { get; set; }
}
[XmlType("InterestRates_E")]
public class InterestRates_E
{
[XmlArray("InterestRate_E")]
[XmlArrayItem("InterestRate_E", typeof(InterestRate_E))]
public InterestRate_E[] InterestRate_E { get; set; }
}
[XmlType("InterestRates_F")]
public class InterestRates_F
{
[XmlArray("InterestRate_F")]
[XmlArrayItem("InterestRate_F", typeof(InterestRate_F))]
public InterestRate_F[] InterestRate_F { get; set; }
}
[Serializable]
public class InterestRate_A
{
[XmlElement("ValidFrom")]
public string ValidFrom { get; set; }
[XmlElement("ValidTo")]
public string ValidTo { get; set; }
[XmlElement("Rate")]
public string Rate { get; set; }
}
[Serializable]
public class InterestRate_B
{
[XmlElement("ValidFrom")]
public string ValidFrom { get; set; }
[XmlElement("ValidTo")]
public string ValidTo { get; set; }
[XmlElement("Rate")]
public string Rate { get; set; }
[XmlElement("Rate2")]
public string Rate2 { get; set; }
}
[Serializable]
public class InterestRate_C
{
[XmlElement("ValidFrom")]
public string ValidFrom { get; set; }
[XmlElement("ValidTo")]
public string ValidTo { get; set; }
[XmlElement("Rate")]
public string Rate { get; set; }
}
[Serializable]
public class InterestRate_D
{
[XmlElement("ValidFrom")]
public string ValidFrom { get; set; }
[XmlElement("ValidTo")]
public string ValidTo { get; set; }
[XmlElement("Rate")]
public string Rate { get; set; }
}
[Serializable]
public class InterestRate_E
{
[XmlElement("ValidFrom")]
public string ValidFrom { get; set; }
[XmlElement("ValidTo")]
public string ValidTo { get; set; }
[XmlElement("Rate")]
public string Rate { get; set; }
}
[Serializable]
public class InterestRate_F
{
[XmlElement("ValidFrom")]
public string ValidFrom { get; set; }
[XmlElement("ValidTo")]
public string ValidTo { get; set; }
[XmlElement("Rate")]
public string Rate { get; set; }
}
}
When I deserialize the XML into my root class InterestRates, all the InterestRate_X collections are empty. How can this be fixed?
The reason deserialization does not work is that you have marked the interest rate arrays in your InterestRates_X types with [XmlArray], e.g.:
[XmlType("InterestRates_B")]
public class InterestRates_B
{
[XmlArray("InterestRate_B")]
[XmlArrayItem("InterestRate_B", typeof(InterestRate_B))]
public InterestRate_B[] InterestRate_B { get; set; }
}
This indicates that the InterestRate_X collections are to be serialized within a container element, like so:
<InterestRates>
<InterestRates_B>
<InterestRate_B> <!--The extra [XmlArray] container element -->
<InterestRate_B>
<ValidFrom>2016-05-01</ValidFrom>
<ValidTo></ValidTo>
<Rate>0.05</Rate>
<Rate2>2.05</Rate2>
</InterestRate_B>
</InterestRate_B>
Since your actual XML does not have this extra level of nesting, deserialization fails.
The solution is to use [XmlElement]:
[XmlType("InterestRates_A")]
public class InterestRates_A
{
[XmlElement("InterestRate_A")]
public InterestRate_A[] InterestRate_A { get; set; }
}
[XmlType("InterestRates_B")]
public class InterestRates_B
{
[XmlElement("InterestRate_B")]
public InterestRate_B[] InterestRate_B { get; set; }
}
[XmlType("InterestRates_C")]
public class InterestRates_C
{
[XmlElement("InterestRate_C")]
public InterestRate_C[] InterestRate_C { get; set; }
}
[XmlType("InterestRates_D")]
public class InterestRates_D
{
[XmlElement("InterestRate_D")]
public InterestRate_D[] InterestRate_D { get; set; }
}
[XmlType("InterestRates_E")]
public class InterestRates_E
{
[XmlElement("InterestRate_E")]
public InterestRate_E[] InterestRate_E { get; set; }
}
[XmlType("InterestRates_F")]
public class InterestRates_F
{
[XmlElement("InterestRate_F")]
public InterestRate_F[] InterestRate_F { get; set; }
}
Sample fiddle.
Alternatively, you could completely eliminate your intermediate InterestRates_X types and merge all your InterestRate_X types into a single type, thereby simplifying your data model as follows:
[XmlRoot("InterestRates")]
public class InterestRates
{
[XmlArray("InterestRates_A")]
[XmlArrayItem("InterestRate_A")]
public InterestRate [] InterestRates_A { get; set; }
[XmlArray("InterestRates_B")]
[XmlArrayItem("InterestRate_B")]
public InterestRate [] InterestRates_B { get; set; }
[XmlArray("InterestRates_C")]
[XmlArrayItem("InterestRate_C")]
public InterestRate [] InterestRates_C { get; set; }
[XmlArray("InterestRates_D")]
[XmlArrayItem("InterestRate_D")]
public InterestRate [] InterestRates_D { get; set; }
[XmlArray("InterestRates_E")]
[XmlArrayItem("InterestRate_E")]
public InterestRate [] InterestRates_E { get; set; }
[XmlArray("InterestRates_F")]
[XmlArrayItem("InterestRate_F")]
public InterestRate [] InterestRates_F { get; set; }
}
public class InterestRate
{
[XmlElement("ValidFrom")]
public string ValidFrom { get; set; }
[XmlElement("ValidTo")]
public string ValidTo { get; set; }
[XmlElement("Rate")]
public string Rate { get; set; }
[XmlElement("Rate2")]
public string Rate2 { get; set; }
}
Sample fiddle #2.

Deserialize JSON to C# Object - No Data being deserialized

Found an odd issue when trying to deserialize a JSON string into a C# Object. It "seems" to perform the operation successfully (as in it does not throw any exception etc), however the outputted POCO contains does not contain any data from the JSON string, it only contains type default data (nulls,"", 0 etc). I have tried this process with other JSON and it works fine.
private string GetJson()
{
return #"{""backdrop_path"":"" / 1LrtAhWPSEetJLjblXvnaYtl7eA.jpg"",""created_by"":[{""id"":488,""name"":""Steven Spielberg"",""profile_path"":"" / pOK15UNaw75Bzj7BQO1ulehbPPm.jpg""},{""id"":31,""name"":""Tom Hanks"",""profile_path"":"" / a14CNByTYALAPSGlwlmfHILpEIW.jpg""}],""episode_run_time"":[60],""first_air_date"":""2001 - 09 - 09"",""genres"":[{""id"":28,""name"":""Action""},{""id"":12,""name"":""Adventure""},{""id"":18,""name"":""Drama""},{""id"":10752,""name"":""War""}],""homepage"":""http://www.hbo.com/band-of-brothers"",""id"":4613,""in_production"":false,""languages"":[""de"",""fr"",""lt"",""nl"",""en""],""last_air_date"":""2001-11-04"",""name"":""Band of Brothers"",""networks"":[{""id"":49,""name"":""HBO""}],""number_of_episodes"":10,""number_of_seasons"":1,""origin_country"":[""GB"",""US""],""original_language"":""en"",""original_name"":""Band of Brothers"",""overview"":""Drawn from interviews with survivors of Easy Company, as well as their journals and letters, Band of Brothers chronicles the experiences of these men from paratrooper training in Georgia through the end of the war. As an elite rifle company parachuting into Normandy early on D-Day morning, participants in the Battle of the Bulge, and witness to the horrors of war, the men of Easy knew extraordinary bravery and extraordinary fear - and became the stuff of legend. Based on Stephen E. Ambrose's acclaimed book of the same name."",""popularity"":3.435181,""poster_path"":""/bUrt6oeXd04ImEwQjO9oLjRguaA.jpg"",""production_companies"":[{""name"":""DreamWorks SKG"",""id"":27},{""name"":""HBO Films"",""id"":7429},{""name"":""DreamWorks Television"",""id"":15258}],""seasons"":[{""air_date"":null,""episode_count"":4,""id"":14071,""poster_path"":""/bMN9iiSAdnmAjflREfCCH0TTNyQ.jpg"",""season_number"":0},{""air_date"":""2001-09-09"",""episode_count"":10,""id"":14070,""poster_path"":""/15SN18OVbYt12Wzttclh51Sz9m1.jpg"",""season_number"":1}],""status"":""Ended"",""type"":""Scripted"",""vote_average"":8.5,""vote_count"":47}";
}
[TestMethod]
public void DeserializeTmdbShowData_ValidShowData_ReturnDeserializedObject()
{
//Arrange
string jsonStream = GetJson();
JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
//Act
var tmdbShowDetails = jsonSerializer.Deserialize<List<TmdbShowDetailsDto>>(jsonStream);
//Assert
Assert.IsNotNull(tmdbShowDetails);
Assert.IsNotNull(tmdbShowDetails.First().backdrop_path);
}
public class TmdbShowDetailsDto
{
public string backdrop_path { get; set; }
public Created_By[] created_by { get; set; }
public int[] episode_run_time { get; set; }
public string first_air_date { get; set; }
public Genre[] genres { get; set; }
public string homepage { get; set; }
public int id { get; set; }
public bool in_production { get; set; }
public string[] languages { get; set; }
public string last_air_date { get; set; }
public string name { get; set; }
public Network[] networks { get; set; }
public int number_of_episodes { get; set; }
public int number_of_seasons { get; set; }
public string[] origin_country { get; set; }
public string original_language { get; set; }
public string original_name { get; set; }
public string overview { get; set; }
public float popularity { get; set; }
public string poster_path { get; set; }
public Production_Companies[] production_companies { get; set; }
public Season[] seasons { get; set; }
public string status { get; set; }
public string type { get; set; }
public float vote_average { get; set; }
public int vote_count { get; set; }
}
public class Created_By
{
public int id { get; set; }
public string name { get; set; }
public string profile_path { get; set; }
}
public class Genre
{
public int id { get; set; }
public string name { get; set; }
}
public class Network
{
public int id { get; set; }
public string name { get; set; }
}
public class Production_Companies
{
public string name { get; set; }
public int id { get; set; }
}
public class Season
{
public string air_date { get; set; }
public int episode_count { get; set; }
public int id { get; set; }
public string poster_path { get; set; }
public int season_number { get; set; }
}
Any ideas - Im sure I have missed something glaringly obvious but I cannot see it. Using .NET 4.5
Help appreciated.
Cheers
I used http://json2csharp.com/, and the code is like this, so you should look at it.
public class Poster
{
public string full { get; set; }
public string medium { get; set; }
public string thumb { get; set; }
}
public class Fanart
{
public string full { get; set; }
public string medium { get; set; }
public string thumb { get; set; }
}
public class Images
{
public Poster poster { get; set; }
public Fanart fanart { get; set; }
}
public class Ids
{
public int trakt { get; set; }
public string slug { get; set; }
public int tvdb { get; set; }
public string imdb { get; set; }
public int tmdb { get; set; }
public int tvrage { get; set; }
}
public class Show
{
public string title { get; set; }
public string overview { get; set; }
public int year { get; set; }
public string status { get; set; }
public Images images { get; set; }
public Ids ids { get; set; }
}
public class Poster2
{
public string full { get; set; }
public string medium { get; set; }
public string thumb { get; set; }
}
public class Fanart2
{
public string full { get; set; }
public string medium { get; set; }
public string thumb { get; set; }
}
public class Images2
{
public Poster2 poster { get; set; }
public Fanart2 fanart { get; set; }
}
public class Ids2
{
public int trakt { get; set; }
public string slug { get; set; }
public string imdb { get; set; }
public int tmdb { get; set; }
}
public class Movie
{
public string title { get; set; }
public string overview { get; set; }
public int year { get; set; }
public Images2 images { get; set; }
public Ids2 ids { get; set; }
}
public class Headshot
{
public object full { get; set; }
public object medium { get; set; }
public object thumb { get; set; }
}
public class Images3
{
public Headshot headshot { get; set; }
}
public class Ids3
{
public int trakt { get; set; }
public string slug { get; set; }
public string imdb { get; set; }
public int tmdb { get; set; }
public int tvrage { get; set; }
}
public class Person
{
public string name { get; set; }
public Images3 images { get; set; }
public Ids3 ids { get; set; }
}
public class RootObject
{
public string type { get; set; }
public object score { get; set; }
public Show show { get; set; }
public Movie movie { get; set; }
public Person person { get; set; }
}
You can use Paste-Special Feature in your VS-IDE Paster JSON as Classes
public class Rootobject
{
public Class1[] Property1 { get; set; }
}
public class Class1
{
public string type { get; set; }
public object score { get; set; }
public Show show { get; set; }
public Movie movie { get; set; }
public Person person { get; set; }
}
public class Show
{
public string title { get; set; }
public string overview { get; set; }
public int year { get; set; }
public string status { get; set; }
public Images images { get; set; }
public Ids ids { get; set; }
}
public class Images
{
public Poster poster { get; set; }
public Fanart fanart { get; set; }
}
public class Poster
{
public string full { get; set; }
public string medium { get; set; }
public string thumb { get; set; }
}
public class Fanart
{
public string full { get; set; }
public string medium { get; set; }
public string thumb { get; set; }
}
public class Ids
{
public int trakt { get; set; }
public string slug { get; set; }
public int tvdb { get; set; }
public string imdb { get; set; }
public int tmdb { get; set; }
public int tvrage { get; set; }
}
public class Movie
{
public string title { get; set; }
public string overview { get; set; }
public int year { get; set; }
public Images1 images { get; set; }
public Ids1 ids { get; set; }
}
public class Images1
{
public Poster1 poster { get; set; }
public Fanart1 fanart { get; set; }
}
public class Poster1
{
public string full { get; set; }
public string medium { get; set; }
public string thumb { get; set; }
}
public class Fanart1
{
public string full { get; set; }
public string medium { get; set; }
public string thumb { get; set; }
}
public class Ids1
{
public int trakt { get; set; }
public string slug { get; set; }
public string imdb { get; set; }
public int tmdb { get; set; }
}
public class Person
{
public string name { get; set; }
public Images2 images { get; set; }
public Ids2 ids { get; set; }
}
public class Images2
{
public Headshot headshot { get; set; }
}
public class Headshot
{
public object full { get; set; }
public object medium { get; set; }
public object thumb { get; set; }
}
public class Ids2
{
public int trakt { get; set; }
public string slug { get; set; }
public string imdb { get; set; }
public int tmdb { get; set; }
public int tvrage { get; set; }
}
Look like you JSON string is not in correct format that you are trying to deserialize. For example i have not find show property in the C# class but it present in JSON.
So this issue I couldn't figure out in the night or early in the morning - it took me till this afternoon until I realised that I was had the wrong JSON!!, so no wonder it wasn't working! I got mixed up with a few different apis that provided JSON. Thanks to those who had pointed out that the class looks wrong, made me double check my code and realise the simplicity of this issue. Kinda needed some rubber ducking to spot the problem. Cheers

How to parse Json from the RootObject?

I am trying to parse JSON response so I created some classes.
Actually I want Leg and Flight class element value. I am trying to get those element value from RootObject but I don't know how to do this. I googled but I am little bit confuse.
I paste my JSON respose , Classes !!
JSON Response :
http://pastebin.com/fjjLxkd2
Classes :
public class Detail
{
}
public class Airport
{
public string kind { get; set; }
public string code { get; set; }
public string city { get; set; }
public string name { get; set; }
}
public class City
{
public string kind { get; set; }
public string code { get; set; }
public string name { get; set; }
}
public class Aircraft
{
public string kind { get; set; }
public string code { get; set; }
public string name { get; set; }
}
public class Tax
{
public string kind { get; set; }
public string id { get; set; }
public string name { get; set; }
}
public class Carrier
{
public string kind { get; set; }
public string code { get; set; }
public string name { get; set; }
}
public class Data
{
public string kind { get; set; }
public List<Airport> airport { get; set; }
public List<City> city { get; set; }
public List<Aircraft> aircraft { get; set; }
public List<Tax> tax { get; set; }
public List<Carrier> carrier { get; set; }
}
public class Flight
{
public string carrier { get; set; }
public string number { get; set; }
}
public class Leg
{
public string kind { get; set; }
public string id { get; set; }
public string aircraft { get; set; }
public string arrivalTime { get; set; }
public string departureTime { get; set; }
public string origin { get; set; }
public string destination { get; set; }
public string originTerminal { get; set; }
public int duration { get; set; }
public int onTimePerformance { get; set; }
public int mileage { get; set; }
public string meal { get; set; }
public bool secure { get; set; }
public string destinationTerminal { get; set; }
public string operatingDisclosure { get; set; }
}
public class Segment
{
public string kind { get; set; }
public int duration { get; set; }
public Flight flight { get; set; }
public string id { get; set; }
public string cabin { get; set; }
public string bookingCode { get; set; }
public int bookingCodeCount { get; set; }
public string marriedSegmentGroup { get; set; }
public List<Leg> leg { get; set; }
public int connectionDuration { get; set; }
}
public class Slouse
{
public string kind { get; set; }
public int duration { get; set; }
public List<Segment> segment { get; set; }
}
public class Fare
{
public string kind { get; set; }
public string id { get; set; }
public string carrier { get; set; }
public string origin { get; set; }
public string destination { get; set; }
public string basisCode { get; set; }
}
public class BagDescriptor
{
public string kind { get; set; }
public string commercialName { get; set; }
public int count { get; set; }
public string subcode { get; set; }
public List<string> description { get; set; }
}
public class FreeBaggageOption
{
public string kind { get; set; }
public List<BagDescriptor> bagDescriptor { get; set; }
public int pieces { get; set; }
}
public class SegmentPricing
{
public string kind { get; set; }
public string fareId { get; set; }
public string segmentId { get; set; }
public List<FreeBaggageOption> freeBaggageOption { get; set; }
}
public class Passengers
{
public string kind { get; set; }
public int adultCount { get; set; }
}
public class Tax2
{
public string kind { get; set; }
public string id { get; set; }
public string chargeType { get; set; }
public string code { get; set; }
public string country { get; set; }
public string salePrice { get; set; }
}
public class Pricing
{
public string kind { get; set; }
public List<Fare> fare { get; set; }
public List<SegmentPricing> segmentPricing { get; set; }
public string baseFareTotal { get; set; }
public string saleFareTotal { get; set; }
public string saleTaxTotal { get; set; }
public string saleTotal { get; set; }
public Passengers passengers { get; set; }
public List<Tax2> tax { get; set; }
public string fareCalculation { get; set; }
public string latestTicketingTime { get; set; }
public string ptc { get; set; }
}
public class TripOption
{
public string kind { get; set; }
public string saleTotal { get; set; }
public string id { get; set; }
public List<Slouse> slice { get; set; }
public List<Pricing> pricing { get; set; }
}
public class Trips
{
public string kind { get; set; }
public string requestId { get; set; }
public Data data { get; set; }
public List<TripOption> tripOption { get; set; }
}
public class RootObject
{
public string kind { get; set; }
public Trips trips { get; set; }
}
Code :
var obj0 = JsonConvert.DeserializeObject<RootObject>(responsedata);
Here I got only Trip class element . I want the Leg and Flight class element.
If you want to find information for a specific leg, you need to use Linq to traverse the tree. For example, if you just know the leg id, you could do something like this:
var allLegs = obj0.trips.tripOption.SelectMany(to => to.slice.SelectMany(sl => sl.segment.Select(sg => sg.leg)));
var leg = allLegs.FirstOrDefault(l => l.id == "yourId");
The query for retrieving a flight would be similar. You could also filter by tripOption id to get a specific tripOption and then retrieve flights or legs that are associated with it.

Categories