Lets say I've got an API response that looks like this
<ApiException>
<Status>400</Status>
<Message>Foot too big for mouth</Message>
<ApiException>
I know how to create a class called ApiException and serialize to that:
public class ApiException
{
public string Status { get; set; }
public string Message { get; set; }
}
using (var response = ((HttpWebResponse)wex.Response))
{
using (var reader = new StreamReader(response.GetResponseStream()))
{
System.Xml.Serialization.XmlSerializer serializer =
new System.Xml.Serialization.XmlSerializer(typeof(ApiException));
ApiException ex = (ApiException)serializer.Deserialize(reader);
}
}
And I also know how to specify Element names for my properties
public class ApiException
{
[System.Xml.Serialization.XmlElement(ElementName = "Status")]
public string Whut { get; set; }
[System.Xml.Serialization.XmlElement(ElementName = "Message")]
public string Why { get; set; }
}
But what if I already have a class called ApiException? Say I want to call this one FootMouthAPIException
Is there any way to do that? Maybe an Data Annotation on the class itself?
You can use the XmlRoot attribute, e.g.
[XmlRoot(ElementName = "ApiException")]
public class FootMouthAPIException
{
}
Related
So i'm getting a response like this
{"$id":"1","success":true,"errors":{"$id":"2","$values":[]}}
how can i convert this into to a c# object, tried using this(http://json2csharp.com/) tool to make an output but it doesn't make sense
this is what i'm getting
x
public class Errors
{
public string __invalid_name__$id { get; set; }
public List<object> __invalid_name__$values { get; set; }
}
public class RootObject
{
public string __invalid_name__$id { get; set; }
public bool success { get; set; }
public Errors errors { get; set; }
}
I'm kinda new to c#, any inputs would be deeply appreciated, i basically need access to success key variable
You need to add [JsonProperty] attribute to every property that key name started with dollar $
public class Errors
{
[JsonProperty("$id")]
public string id { get; set; }
[JsonProperty("$values")]
public List<object> values { get; set; }
}
public class RootObject
{
[JsonProperty("$id")]
public string id { get; set; }
public bool success { get; set; }
public Errors errors { get; set; }
}
Because the $ indicates metadata, not an actual data field. so you have to modify your JsonSerializerSettings to ignore MetadataPropertyHandling.
JsonSerializerSettings settings = new JsonSerializerSettings();
settings.MetadataPropertyHandling = MetadataPropertyHandling.Ignore;
And finally deserialize your json to above class objects.
RootObject rootObject = JsonConvert.DeserializeObject<RootObject>(json, settings);
Here I created a sample console app for demonstration purpose that shows how above code will work.
class program
{
public static void Main()
{
string json = File.ReadAllText(#"Path to your json file");
JsonSerializerSettings settings = new JsonSerializerSettings();
settings.MetadataPropertyHandling = MetadataPropertyHandling.Ignore;
RootObject rootObject = JsonConvert.DeserializeObject<RootObject>(json, settings);
Console.WriteLine("id: " + rootObject.id);
Console.WriteLine("success: " + rootObject.success);
Console.WriteLine("errors.id: " + rootObject.errors.id);
Console.WriteLine("errors.values: " + string.Join(",", rootObject.errors.values));
Console.ReadLine();
}
}
Output:
Well, What you can do is
public class Errors
{
[JsonProperty(PropertyName = "$id")]
public string id { get; set; }
[JsonProperty(PropertyName = "$values")]
public List<object> values { get; set; }
}
public class RootObject
{
[JsonProperty(PropertyName = "$id")]
public string id { get; set; }
public bool success { get; set; }
public Errors errors { get; set; }
}
You need your object attributes to match you json string ($id instead of _invalid_name_$id), then you can use:
JsonConvert.DeserializeObject<RootObject>(jsonString);
Here is a simple class to serialize json string from object or to object (T). May de/serialize array(list) of objects.
public class HelperSerializer<T> where T: class
{
public static string WriteFromObject(T source)
{
using (var ms = new MemoryStream()) {
var ser = new DataContractJsonSerializer(typeof(T));
ser.WriteObject(ms, source);
byte[] json = ms.ToArray();
return Encoding.UTF8.GetString(json, 0, json.Length);
}
}
// Deserialize a JSON stream to an object.
public static T ReadToObject(string json)
{
using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(json)))
{
var ser = new DataContractJsonSerializer(typeof(T));
return ser.ReadObject(ms) as T;
}
}
}
Use persons = HelperSerializer<List<Person>>.ReadToObject(json);
and var json = HelperSerializer<List<Person>>.WriteFromObject(persons);
i have part of xml document
<Tender SubTenderType="BC" TenderType="Check">
<TenderTotal>
<Amount>10.00</Amount>
</TenderTotal>
</Tender>
i need convert it to class.
public class Tender
{
public string SubTenderType { get; set; }
public string TenderType { get; set; }
public decimal Amount { get; set; }
}
what i already wrote and this work. but i interseing can i deserialize xml to class as written above?
[Serializable]
public class Tender
{
[XmlAttribute("SubTenderType")]
public string SubTenderType { get; set; }
[XmlAttribute("TenderType")]
public string TenderType { get; set; }
[XmlElement("TenderTotal")]
public TenderTotal TenderTotal { get; set; }
}
[Serializable]
public class TenderTotal
{
[XmlElement("Amount")]
public decimal Amount { get; set; }
}
You can deserialize xml to first Type "Tender" and next use autoMapper to map your type (create new object of different type)
create map:
config.CreateMap<TenderFirst, TenderSecond>().ForMember(x => x.TenderTotal.Amount, y => y.Amount ())
Having the following class without XmlAttribute:
public class Tender
{
public string SubTenderType { get; set; }
public string TenderType { get; set; }
public decimal Amount { get; set; }
}
You can use the XmlAttributeOverrides class to override the behavior of the serializer in such a way that instead of elements it would do the attributes.
var attrSTT = new XmlAttributes { XmlAttribute = new XmlAttributeAttribute("SubTenderType") };
var attrTT = new XmlAttributes { XmlAttribute = new XmlAttributeAttribute("TenderType") };
var overrides = new XmlAttributeOverrides();
overrides.Add(typeof(Tender), nameof(Tender.SubTenderType), attrSTT);
overrides.Add(typeof(Tender), nameof(Tender.TenderType), attrTT);
var xs = new XmlSerializer(typeof(Tender), overrides);
However, in this way impossible add a new item or wrap one element in another.
Therefore, you have to do custom serialization, or map one type to another, or writing a custom xml reader/writer, or perform the read/write manually (for example, using linq2xml). There are many ways...
I have a merchant account balance request class as follows:
[Serializable]
[XmlRoot(ElementName = "accountbalance", Namespace = "", IsNullable = false)]
public class MerchantAccountBalanceRequest
{
[XmlElementAttribute("agent")]
public string Agent { get; set; }
[XmlElementAttribute("agentPin")]
public string AgentPin { get; set; }
}
Which will result in the following XML:
<accountbalance>
<agent>aaaaaa</agent>
<agentPin>mmmmmm</agentPin>
</accountbalance>
Could I somehow put an attribute on my class so that for the MerchantAccountBalanceRequest object I will get the following XML:
<Envelope>
<Body>
<accountbalance>
<agent>aaaaaa</agent>
<agentPin>mmmmmm</agentPin>
</accountbalance>
</Body>
</Envelope>
That is, just wrap it up with Envelope and Body elements.
Whilst you can't achieve this with an attribute on your request class, you can wrap your request up in other classes representing the Envelope and Body elements:
public class MerchantAccountBalanceRequest
{
[XmlElement("agent")]
public string Agent { get; set; }
[XmlElement("agentPin")]
public string AgentPin { get; set; }
}
public class RequestBody
{
[XmlElement("accountbalance")]
public MerchantAccountBalanceRequest BalanceRequest { get; set; }
}
[XmlRoot(ElementName = "Envelope")]
public class RequestEnvelope
{
[XmlElement("Body")]
public RequestBody Body { get; set; }
}
When serializing and deserializing, construct your XmlSerializer object from RequestEnvelope instead of MerchantAccountBalanceRequest. You will have to set the RequestEnvelope.Body and RequestBody.BalanceRequest properties when constructing them (you could probably initialize the Body property in RequestEnvelope to a new RequestBody instance in the constructor, for convenience).
This looks like a single request type in some protocol, so if you have multiple request types, you could create a base-class for the requests (e.g. RequestBase), and adjust the RequestBody so that it accepts a choice of the possible request elements like in the following example:
public abstract class RequestBase
{
}
public class SomeOtherRequest : RequestBase
{
[XmlElementAttribute("example")]
public string Example { get; set; }
}
public class MerchantAccountBalanceRequest : RequestBase
{
[XmlElement("agent")]
public string Agent { get; set; }
[XmlElement("agentPin")]
public string AgentPin { get; set; }
}
public class RequestBody
{
[XmlElement(ElementName = "accountbalance", Type = typeof(MerchantAccountBalanceRequest))]
[XmlElement(ElementName = "somethingelse", Type = typeof(SomeOtherRequest))]
public RequestBase Request { get; set; }
}
I am currently developing a client library for connecting to Newegg using the documentation provided by Newegg and have a question on class design.
In working with various API's ( namely NetSuite and Amazon's MWS ) I come across classes that have are used like this:
recToFulfill.packageList = new ItemFulfillmentPackageList();
recToFulfill.packageList.package = new ItemFulfillmentPackage[ifitemlist.item.Length];
recToFulfill.packageList.package[i] = new ItemFulfillmentPackage();
recToFulfill.packageList.package[i].packageWeightSpecified = true;
recToFulfill.packageList.package[i].packageTrackingNumber = "trackingNumber";
The question I have is: How do I properly design the nested objects like above? I have never had to worry about this previously, so I am unsure on where to look, or start.
The bit I need to figure out looks like this ( taken from the API documentation provided):
<UpdateOrderStatusInfo>
<IsSuccess></IsSuccess>
<Result>
<OrderNumber></OrderNumber>
<SellerID></SellerID>
<OrderStatus></OrderStatus>
</Result>
</UpdateOrderStatusInfo>
All fields are type string, except order number which is an integer.
I have this currently:
public class UpdateOrderStatusInfo
{
public string IsSuccess { get; set; }
public int OrderNumber { get; set; }
public string SellerID { get; set; }
public string OrderStatus { get; set; }
}
But the returned XML Response has Results as a parent node which to me seems like it should be represented within the class itself. Would I just do this?
public UpdateOrderStatusInfo results {get; set;}
If so, where do the child nodes go?
What I need is to be able to say is something like:
UpdateOrderStatusInfo updateInfo = new UpdateOrderStatusInfo();
if(updateInfo.IsSuccess.Equals("true")
{
Console.WriteLine(updateInfo.Results.OrderStatus);
}
Any help, or advice on where to get this information is appreciated.
Easy breezy. If it has no children, it's a scalar property. If it does, it is its own class, and referenced in the parent class accordingly. If it repeats, it's a collection, and is referenced like a class (these are complex type, not primitives). Make sure you initialize them in your constructors).
class Program
{
static void Main(string[] args)
{
var myOrder = new UpdateOrderStatusInfo();
myOrder.IsSuccess = "true";
myOrder.OrderResult.OrderNumber = 1001;
myOrder.OrderResult.OrderStatus = "Pending";
myOrder.OrderResult.SellerID = "69";
}
}
public class UpdateOrderStatusInfo
{
public string IsSuccess { get; set; }
public Result OrderResult { get; set; }
public UpdateOrderStatusInfo()
{
OrderResult = new Result();
}
}
public class Result
{
public int OrderNumber { get; set; }
public string SellerID { get; set; }
public string OrderStatus { get; set; }
}
You need to define the Result as a separate class, called whatever you want, then add a Result property as that type. The Result class can be defined at the namespace level, or, if you are unlikely to use it anywhere else on its own, you can nest the class definition inside the UpdateOrderStatusInfo class:
public class UpdateOrderStatusInfo
{
public class UpdateOrderResult
{
public int OrderNumber { get; set; }
public string SellerID { get; set; }
public string OrderStatus { get; set; }
}
public UpdateOrderStatusInfo()
{
Result = new UpdateOrderResult();
}
public string IsSuccess { get; set; }
public UpdateOrderResult Result { get; set; }
}
The easy way is to use the xsd.exe tool.
The command xsd response.xml will generate the file response.xsd
The command xsd response.xsd /C will generate the file response.cs which contains the classes necessary to serialize/deserialize the xml posted.
I am trying to serialize an object but I am facing some issues regarding the attributes of a parent element that contains an array.
I have the following xml structure and I can't add the attribute in RatePlans element.
<Root>
<RatePlans Attribute="??this one??">
<RatePlan Attribute1="RPC" Attribute2="MC" Attribute3="RPT">
.
.
.
</RatePlan>
<RatePlan Attribute1="RPC2" Attribute2="MC3" Attribute3="RPT4">
.
.
.
</RatePlan>
</RatePlans>
</Root>
This is what I have done so far:
namespace XmlT {
[Serializable]
[XmlRoot("Root")]
public class Root {
public List<RatePlan> RatePlans { get; set; }
}
}
namespace XmlT {
[Serializable]
public class RatePlan {
[XmlAttribute]
public string RatePlanCode { get; set; }
[XmlAttribute]
public string MarketCode { get; set; }
[XmlAttribute]
public string RatePlanType { get; set; }
}
}
This gives me a correct structure but I don't know how to add the attribute I want
Another approach
I've tried also another approach but this gives me wrong values at all.
namespace XmlT {
[Serializable]
[XmlRoot("Root")]
public class Root {
public RatePlans RatePlans { get; set; }
}
}
namespace XmlT {
[Serializable]
public class RatePlans {
[XmlAttribute]
public string HotelCode { get; set; }
public List<RatePlan> RatePlan { get; set; }
}
}
EDIT
this the method that I am using for the serialization
protected static string Serialize<T>(object objToXml, bool IncludeNameSpace = false) where T : class {
StreamWriter stWriter = null;
XmlSerializer xmlSerializer;
string buffer;
try {
xmlSerializer = new XmlSerializer(typeof(T));
MemoryStream memStream = new MemoryStream();
stWriter = new StreamWriter(memStream);
if (!IncludeNameSpace) {
var xs = new XmlSerializerNamespaces();
xs.Add("", "");
xmlSerializer.Serialize(stWriter, objToXml, xs);
} else {
xmlSerializer.Serialize(stWriter, objToXml);
}
buffer = Encoding.ASCII.GetString(memStream.GetBuffer());
} catch (Exception Ex) {
throw Ex;
} finally {
if (stWriter != null) stWriter.Close();
}
return buffer;
}
Does anyone know how could I do this?
Thanks
If the RatePlans class from the 2nd example inherits from List<RatePlan> you will get the desired result:
[Serializable]
public class RatePlans: List<RatePlan>
{
[XmlAttribute]
public string HotelCode { get; set; }
}
Edit:
My bad. Fields of classes inheriting from collections will not be serialized. I didn't knew that. Sorry...
However, this solution works:
[Serializable]
[XmlRoot("Root")]
public class Root
{
public RatePlans RatePlans { get; set; }
}
[Serializable]
public class RatePlans
{
[XmlAttribute]
public string HotelCode { get; set; }
[XmlElement("RatePlan")]
public List<RatePlan> Items = new List<RatePlan>();
}