C# XMLNS complicated deserialization - c#

I'm trying to deserialize XMLNS file.
<feed xmlns:live="http://www.live.com/marketplace" xmlns="http://www.w3.org/2005/Atom">
<live:totalItems>1177</live:totalItems>
<live:numItems>1</live:numItems>
<title>FindGames Results</title>
<updated>2013-09-19T09:28:02.77Z</updated>
<entry live:itemNum="34" live:detailView="3">
<id>urn:uuid:66ACD000-77FE-1000-9115-D802555308C3</id>
<updated>2013-09-19T09:28:02.77Z</updated>
<title>Rayman® Legends</title>
<content type="text">game content</content>
<live:media>
<live:mediaType>1</live:mediaType>
<live:gameTitleMediaId>urn:uuid:66ACD000-77FE-1000-9115-D802555308C3</live:gameTitleMediaId>
<live:reducedTitle>Rayman® Legends</live:reducedTitle>
<live:reducedDescription>The Glade of Dreams is in trouble once again! During a 100-year nap, nightmares have multiplied and spread, creating new monsters even scarier than before!</live:reducedDescription>
<live:availabilityDate>2013-06-11T00:00:00</live:availabilityDate>
<live:releaseDate>2013-08-29T00:00:00</live:releaseDate>
<live:ratingId>20</live:ratingId>
<live:developer>Ubisoft</live:developer>
<live:publisher>Ubisoft</live:publisher>
<live:newestOfferStartDate>2013-09-13T09:00:00Z</live:newestOfferStartDate>
<live:totalOfferCount>1</live:totalOfferCount>
<live:titleId>1431505091</live:titleId>
<live:effectiveTitleId>1431505091</live:effectiveTitleId>
<live:gameReducedTitle>Rayman® Legends</live:gameReducedTitle>
<live:ratingAggregate>4.50</live:ratingAggregate>
<live:numberOfRatings>1193</live:numberOfRatings>
</live:media>
</entry>
</feed>
My current code:
Deserialize class:
[XmlRoot("feed", Namespace = "http://www.w3.org/2005/Atom")]
public class entry
{
public string title { get; set; }
public string totalItems { get; set; }
public string reducedDescription{ get; set; }
public string ratingId { get; set; }
public string developer { get; set; }
public string publisher { get; set; }
public string tittleId { get; set; }
public string ratingAggregate { get; set; }
public string numberOfRatings { get; set; }
public string boxImage { get; set; }
public string categories { get; set; }
}
class deserializeXML
{
public static void deserialize()
{
using (StreamReader reader = new StreamReader("Query.xml"))
{
XmlSerializer serializer = new XmlSerializer(typeof(entry));
entry x1 = serializer.Deserialize(reader) as entry;
}
}
}
I just receiving title (FindGames Results instead of Rayman® Legends).
I need to get parameters after "entry" and "live:media", i have no idea how to receive parameters with "live:" at the beginning.
EDIT:
[XmlElement(Namespace = "http://www.live.com/marketplace")]
public int numItems { get; set; }
[XmlElement(Namespace = "http://www.live.com/marketplace")]
public int totalItems { get; set; }
It's working well, but:
[XmlElement("media" Namespace="http://www.live.com/marketplace")]
public media media{ get; set; }
This returning null media class :/

You need to explicitly map those types and properties that are not in the atom namespace to their respective namespaces. The namespace declaration in the XmlRoot just specifies the namespace of the root element. If you have multiple namespaces in the document you need to call them out explicitly.
For instance to map the itemNum attribute correctly try this in your entry class:
[XmlRoot("feed", Namespace = "http://www.w3.org/2005/Atom")]
public class entry
{
[XmlAttribute("itemNum", Namespace = "http://www.live.com/marketplace")]
public int itemNum { get; set; }
public string title { get; set; }
public string totalItems { get; set; }
public string reducedDescription { get; set; }
public string ratingId { get; set; }
public string developer { get; set; }
public string publisher { get; set; }
public string tittleId { get; set; }
public string ratingAggregate { get; set; }
public string numberOfRatings { get; set; }
public string boxImage { get; set; }
public string categories { get; set; }
}
You then need to declare additional type to map into things like the media node.
class media
{
public int mediaType{ get; set; }
public string reducedDescription{ get; set; }
etc. etc.
}
You then need to move all of those "live" properties from your entry class into you new media class leaving you with an entry class that might look like this:
[XmlRoot("feed", Namespace = "http://www.w3.org/2005/Atom")]
public class entry
{
[XmlAttribute("itemNum", Namespace = "http://www.live.com/marketplace")]
public int itemNum { get; set; }
public string title { get; set; }
[XmlElement("media" Namespace="http://www.live.com/marketplace")]
public media media{ get; set; }
}
Your class hierarchy does not need to exactly match the xml hierarchy and you can do some significant remapping and transformation of the Xml using the various attributes in System.Xml.Serialization however I find it easier to implement, understand and maintain this sort of stuff if the C# classes map 1:1 onto the xml structure.

Related

Deserializing Json from iTunes store into C# object

I have the following Json returned from iTunes (I have replaced some details for privacy such as username and review content)
{"feed":{"author":{"name":{"label":"iTunes Store"}, "uri":{"label":"http://www.apple.com/uk/itunes/"}}, "entry":[
{"author":{"uri":{"label":"https://itunes.apple.com/gb/reviews/ID"}, "name":{"label":"Username"}, "label":""}, "im:version":{"label":"3.51"}, "im:rating":{"label":"4"}, "id":{"label":"12345"}, "title":{"label":"Review title"}, "content":{"label":"Review contents", "attributes":{"type":"text"}}, "link":{"attributes":{"rel":"related", "href":"https://itunes.apple.com/gb/review?reviewid"}}, "im:voteSum":{"label":"0"}, "im:contentType":{"attributes":{"term":"Application", "label":"Application"}}, "im:voteCount":{"label":"0"}},
// more entries ...
I want to deserialize this into a class. I only need the Review Title, Review contents, and the "im:rating" (which is the number of stars). However, I'm struggling due to the nested Json and the use of keys of how to extract this data. So far I have created a class ready to deserialize, and I have AppStoreData appStoreData = JsonConvert.DeserializeObject<AppStoreData>(stringResult); to deserialize it
public class AppStoreData {
}
My problem is that I don't know what to put inside the class to get the info I need from the Json. I have tried things such as:
public string title {get; set;}
public string content {get; set;}
public string imrating {get; set;}
However this doesn't work. I also think im:rating is an invalid name in C#.
Can anyone please help? Thank you
To get around the issue with im:rating you can use a JsonProperty attribute
[JsonProperty("im:rating")]
public Id ImRating { get; set; }
The issue with converting things like Label to string property is that it's not a string but an object in the input file.
You need something like
[JsonProperty("title")]
public Id Title { get; set; }
where Id is a class
public class Id
{
[JsonProperty("label")]
public string Label { get; set; }
}
Or write a deserializer that converts it to string for you.
I would suggest trying out one of many free code generators like https://app.quicktype.io/?l=csharp or http://json2csharp.com/ to get a headstart and edit everything to your liking afterwards.
I suggest using json2csharp to convert the json to c# model and change the invalid attributes by adding JsonProperty
public class Name
{
public string label { get; set; }
}
public class Uri
{
public string label { get; set; }
}
public class Author
{
public Name name { get; set; }
public Uri uri { get; set; }
}
public class Uri2
{
public string label { get; set; }
}
public class Name2
{
public string label { get; set; }
}
public class Author2
{
public Uri2 uri { get; set; }
public Name2 name { get; set; }
public string label { get; set; }
}
public class ImVersion
{
public string label { get; set; }
}
public class ImRating
{
public string label { get; set; }
}
public class Id
{
public string label { get; set; }
}
public class Title
{
public string label { get; set; }
}
public class Attributes
{
public string type { get; set; }
}
public class Content
{
public string label { get; set; }
public Attributes attributes { get; set; }
}
public class Attributes2
{
public string rel { get; set; }
public string href { get; set; }
}
public class Link
{
public Attributes2 attributes { get; set; }
}
public class ImVoteSum
{
public string label { get; set; }
}
public class Attributes3
{
public string term { get; set; }
public string label { get; set; }
}
public class ImContentType
{
public Attributes3 attributes { get; set; }
}
public class ImVoteCount
{
public string label { get; set; }
}
public class Entry
{
public Author2 author { get; set; }
[JsonProperty(PropertyName = "im:version")]
public ImVersion imversion { get; set; }
[JsonProperty(PropertyName = "im:rating")]
public ImRating imrating { get; set; }
public Id id { get; set; }
public Title title { get; set; }
public Content content { get; set; }
public Link link { get; set; }
[JsonProperty(PropertyName = "im:voteSum")]
public ImVoteSum imvoteSum { get; set; }
[JsonProperty(PropertyName = "im:contentType")]
public ImContentType imcontentType { get; set; }
[JsonProperty(PropertyName = "im:voteCount")]
public ImVoteCount imvoteCount { get; set; }
}
public class Feed
{
public Author author { get; set; }
public List<Entry> entry { get; set; }
}
public class RootObject5
{
public Feed feed { get; set; }
}
Once the model is ready you can use JsonConvert.DeserializeObject to convert the JSON to c# object
using (StreamReader r = new StreamReader(filepath)) {
string json = r.ReadToEnd();
var newEmps = JsonConvert.DeserializeObject<RootObject5>(json);
Console.WriteLine(newEmps.feed.entry[0].imvoteCount.label);
}

XML string array deserialize

I Have a XML string
<ItemAttributes>
<Binding>Misc.</Binding>
<Brand>Gilbert</Brand>
<Department>mens</Department>
<Feature>Elasticated waist with drawcord</Feature>
<Feature>Two pockets with reinforced stitching at base</Feature>
<Feature>Reinforced seams for strength in wear</Feature>
<Feature>Off set inside leg seam to reduce chaffing</Feature>
<Feature>100% Cotton Twill</Feature>
</ItemAttributes>
and class
[Serializable()]
public class ItemAttributes
{
public string Binding { get; set; }
public string Brand { get; set; }
public string Color { get; set; }
[XmlArrayItem("Feature")]
public string[] Feature { get; set; }
}
When I deserialize xml to object, "Feature" has no value at all. I want it to be an array of strings.
Try this:
[Serializable]
public class ItemAttributes
{
public string Binding { get; set; }
public string Brand { get; set; }
public string Color { get; set; }
[XmlElement(ElementName = "Feature")]
public string[] Feature { get; set; }
}

AutoMapper - object to object different namespaces

I have a scenario where we have two services that reference an object definition dll which is called SLInterface.dll. This is just a compilation of our object model. This object is huge so I'll tone it down and give a snippet of the code:
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.ComponentModel;
namespace SLInterface
{
[DataContract(Namespace = Utilities.strNamespace)]
public class EntityShipment
{
public EntityShipment()
{
lstEntityResponseStatus = new List<EntityResponseStatus>();
}
[DataMember]
public EntityAddress objShipTo
{
get;
set;
}
[DataMember]
public EntityToolKit ToolKit { get; set; }
[DataMember]
public List<EntityResponseStatus> lstEntityResponseStatus
{
get;
set;
}
}
}
namespace SLInterface
{
public class EntityAddress
{
[DataMember]
public String strCustomerID
{
get;
set;
}
[DataMember]
public String strCompanyName
{
get;
set;
}
[DataMember]
public String strContactName
{
get;
set;
}
[DataMember]
public String strAddressLine1
{
get;
set;
}
[DataMember]
public String strAddressLine2
{
get;
set;
}
[DataMember]
public String strAddressLine3
{
get;
set;
}
[DataMember]
public String strCity
{
get;
set;
}
[DataMember]
public String strState
{
get;
set;
}
[DataMember]
public String strPostalCode
{
get;
set;
}
[DataMember]
public String strCountryCode
{
get;
set;
}
[DataMember]
public String strCountryName
{
get;
set;
}
[DataMember]
public String strPhoneNumber
{
get;
set;
}
[DataMember]
public String strFaxNumber
{
get;
set;
}
[DataMember]
public Boolean blnRBDICheck
{
get;
set;
}
[DataMember]
public Boolean blnIsRBDI
{
get;
set;
}
[DataMember]
public String strPlantID
{
get;
set;
}
[DataMember]
public String strAccountNumber
{
get;
set;
}
[DataMember]
public String strEmailAddress
{
get;
set;
}
[DataMember]
public Boolean blnBlindShip
{
get;
set;
}
}
}
And Then there is another definition for toolkit and for EntityResponseStatus which I can post if it is needed.
We have a scenario where from ServiceA, we need to pass this object to ServiceB. We can't just pass this object that is filled during ServiceA as it is a different namespace. So we're trying to use AutoMapper so we don't have to map each property and it values individually.
Here is our AutoMapperCode:
public class ES
{
public ServiceA.SLInterface.EntityResponseStatus CallES(string strPrinterPath, string strShipmentNumber, string strCompanyCode)
{
Mapper.CreateMap<ServiceA.SLInterface.EntityShipment, ServiceB.SLInterface.EntityShipment>();
ServiceA.SLInterface.EntityResponseStatus objERS = new ServiceA.SLInterface.EntityResponseStatus();
objERS.StatusType = ServiceA.SLInterface.ResponseStatusType.SUCCESS;
ServiceA.SLInterface.EntityShipment objES = new ServiceA.SLInterface.EntityShipment();
objES = FillES(objES, strShipmentNumber, strCompanyCode);
Sage300 obj300 = new Sage300();
objES = obj300.Pull(objES);
ServiceB.SLInterface.EntityShipment objESES = new ServiceB.SLInterface.EntityShipment();
try
{
objESES = Mapper.Map<ServiceB.SLInterface.EntityShipment>(objES); //This is the line that we blow up on. The error message is "Missing type map configuration or unsupported mapping."
}
catch(AutoMapperConfigurationException amce)
{
ServiceA.SLInterface.EntityResponseStatus objERS1 = new ServiceA.SLInterface.EntityResponseStatus();
objERS1.Message = amce.Message;
}
catch (AutoMapperMappingException amme)
{
ServiceA.SLInterface.EntityResponseStatus objERS1 = new ServiceA.SLInterface.EntityResponseStatus();
objERS1.Message = amme.Message;
}
objESES = ServiceB.CallESShip(objESES,strPrinterPath);
return objERS;
}
In the try catch is where we get the error message
objESES = Mapper.Map<ServiceB.SLInterface.EntityShipment>(objES); //This is the line that we blow up on. The error message is "Missing type map configuration or unsupported mapping."
Both object models are exactly the same. Do we have to do a mapping for each object in the main object. Right now, we are thinking with the code that we have that it would be mapping everything from the root down exactly the same and then transferring over the data. Any help would be appreciated.
Thanks!

C# deserialization CBISDDStsRptPhyMsg

I need to deserialize a XML that follows the CBI Italian standard, the problem is that I've already marked every class with the namespace's but I'm still unable to deserialize.
This is part of the xml that I'm trying to deserialize (content erased):
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<RISPOSTASDD:CBISDDStsRptPhyMsg xmlns:HE2E="urn:CBI:xsd:CBIHdrSrv.001.07" xmlns:BODY="urn:CBI:xsd:CBIBdySDDStsRpt.00.01.00" xmlns:SGNT="urn:CBI:xsd:CBISgnInf.001.04" xmlns:RISPOSTASDD="urn:CBI:xsd:CBISDDStsRptPhyMsg.00.01.00" xmlns:LMSG="urn:CBI:xsd:CBISDDStsRptLogMsg.00.01.00" xmlns:HTRT="urn:CBI:xsd:CBIHdrTrt.001.07">
<RISPOSTASDD:CBIHdrTrt>
<HTRT:IdCBISndrf></HTRT:IdCBISndrf>
<HTRT:IdCBIRcvrf></HTRT:IdCBIRcvrf>
<HTRT:SrvNm></HTRT:SrvNm>
<HTRT:IdMsgTrt></HTRT:IdMsgTrt>
<HTRT:XMLCrtDt></HTRT:XMLCrtDt>
<HTRT:RtrnAddrl></HTRT:RtrnAddrl>
</RISPOSTASDD:CBIHdrTrt>
<RISPOSTASDD:CBIHdrSrv>
<HE2E:SrvInfo>
<HE2E:SrvNm></HE2E:SrvNm>
<HE2E:IdE2EMsg></HE2E:IdE2EMsg>
<HE2E:XMLCrtDt></HE2E:XMLCrtDt>
</HE2E:SrvInfo>
<HE2E:Sender>
<HE2E:IdCBISend></HE2E:IdCBISend>
<HE2E:SendTyp></HE2E:SendTyp>
<HE2E:CBIRefrSend></HE2E:CBIRefrSend>
</HE2E:Sender>
<HE2E:Receiver>
<HE2E:IdCBIRecv></HE2E:IdCBIRecv>
<HE2E:RecvTyp></HE2E:RecvTyp>
<HE2E:CBIRefrRecv></HE2E:CBIRefrRecv>
</HE2E:Receiver>
<HE2E:DiagInfo>
<HE2E:UsrBnk></HE2E:UsrBnk>
<HE2E:DiagVers></HE2E:DiagVers>
<HE2E:ChkSbj></HE2E:ChkSbj>
<HE2E:ChkDt></HE2E:ChkDt>
</HE2E:DiagInfo>
<HE2E:CongrInfo>
<HE2E:SrvBdyNb></HE2E:SrvBdyNb>
</HE2E:CongrInfo>
</RISPOSTASDD:CBIHdrSrv>
<RISPOSTASDD:CBIBdySDDStsRpt>
<BODY:PhyMsgInf>
<BODY:PhyMsgTpCd></BODY:PhyMsgTpCd>
<BODY:NbOfLogMsg></BODY:NbOfLogMsg>
</BODY:PhyMsgInf>
<BODY:CBIEnvelSDDStsRptLogMsg>
<BODY:CBISDDStsRptLogMsg>...
And these are (some) the classes that I've wrote to deserialize it:
[Serializable, XmlRoot(Namespace = "urn:CBI:xsd:CBISDDStsRptPhyMsg.00.01.00")]
public class CBISDDStsRptPhyMsg
{
[XmlElement("CBIHdrTrt", Namespace = "urn:CBI:xsd:CBIHdrTrt.001.07")]
public CBIHdrTrt CBIHdrTrt {get;set;}
[XmlElement("CBIHdrSrv", Namespace="urn:CBI:xsd:CBIHdrSrv.001.07")]
public CBIHdrSrv CBIHdrSrv {get;set;}
[XmlElement("CBIBdySDDStsRpt", Namespace="urn:CBI:xsd:CBIBdySDDStsRpt.00.01.00")]
public CBIBdySDDStsRpt CBIBdySDDStsRpt { get; set; }
}
[Serializable]
public class CBIHdrTrt
{
[XmlElement("IdCBISndrf", Namespace = "urn:CBI:xsd:CBIHdrTrt.001.07")]
public string IdCBISndrf { get; set; }
[XmlElement("IdCBIRcvrf", Namespace = "urn:CBI:xsd:CBIHdrTrt.001.07")]
public string IdCBIRcvrf { get; set; }
[XmlElement("SrvNm", Namespace = "urn:CBI:xsd:CBIHdrTrt.001.07")]
public string SrvNm { get; set; }
[XmlElement("IdMsgTrt", Namespace = "urn:CBI:xsd:CBIHdrTrt.001.07")]
public string IdMsgTrt { get; set; }
[XmlElement("XMLCrtDt", Namespace = "urn:CBI:xsd:CBIHdrTrt.001.07")]
public DateTime XMLCrtDt { get; set; }
[XmlElement("RtrnAddrl", Namespace = "urn:CBI:xsd:CBIHdrTrt.001.07")]
public string RtrnAddrl { get; set; }
}
[Serializable]
public class CBIHdrSrv
{
[XmlElement("SrvInfo",Namespace="urn:CBI:xsd:CBIHdrSrv.001.07")]
public SrvInfo SrvInfo { get; set; }
[XmlElement("Sender",Namespace="urn:CBI:xsd:CBIHdrSrv.001.07")]
public Sender Sender { get; set; }
[XmlElement("Receiver",Namespace="urn:CBI:xsd:CBIHdrSrv.001.07")]
public Receiver Receiver { get; set; }
[XmlElement("DiagInfo",Namespace="urn:CBI:xsd:CBIHdrSrv.001.07")]
public DiagInfo DiagInfo { get; set; }
[XmlElement("CongrInfo",Namespace="urn:CBI:xsd:CBIHdrSrv.001.07")]
public CongrInfo CongrInfo { get; set; }
}
[Serializable]
public class SrvInfo
{
[XmlElement("SrvNm",Namespace="urn:CBI:xsd:CBIHdrSrv.001.07")]
public string SrvNm { get; set; }
[XmlElement("IdE2EMsg",Namespace="urn:CBI:xsd:CBIHdrSrv.001.07")]
public string IdE2EMsg { get; set; }
[XmlElement("XMLCrtDt",Namespace="urn:CBI:xsd:CBIHdrSrv.001.07")]
public DateTime XMLCrtDt { get; set; }
}
[Serializable]
public class Sender
{
[XmlElement("IdCBISend",Namespace="urn:CBI:xsd:CBIHdrSrv.001.07")]
public string IdCBISend { get; set; }
[XmlElement("SendTyp",Namespace="urn:CBI:xsd:CBIHdrSrv.001.07")]
public string SendTyp { get; set; }
[XmlElement("CBIRefrSend",Namespace="urn:CBI:xsd:CBIHdrSrv.001.07")]
public string CBIRefrSend { get; set; }
}
[Serializable]
public class Receiver
{
[XmlElement("IdCBIRecv",Namespace="urn:CBI:xsd:CBIHdrSrv.001.07")]
public string IdCBIRecv { get; set; }
[XmlElement("RecvTyp",Namespace="urn:CBI:xsd:CBIHdrSrv.001.07")]
public string RecvTyp { get; set; }
[XmlElement("CBIRefrRecv",Namespace="urn:CBI:xsd:CBIHdrSrv.001.07")]
public string CBIRefrRecv { get; set; }
}
[Serializable]
public class DiagInfo
{
[XmlElement("UsrBnk",Namespace="urn:CBI:xsd:CBIHdrSrv.001.07")]
public string UsrBnk { get; set; }
[XmlElement("DiagVers",Namespace="urn:CBI:xsd:CBIHdrSrv.001.07")]
public string DiagVers { get; set; }
[XmlElement("ChkSbj",Namespace="urn:CBI:xsd:CBIHdrSrv.001.07")]
public string ChkSbj { get; set; }
[XmlElement("ChkDt",Namespace="urn:CBI:xsd:CBIHdrSrv.001.07")]
public DateTime ChkDt { get; set; }
}
[Serializable]
public class CongrInfo
{
[XmlElement("SrvBdyNb",Namespace="urn:CBI:xsd:CBIHdrSrv.001.07")]
public int SrvBdyNb { get; set; }
}
[Serializable]
public class CBIBdySDDStsRpt
{
[XmlElement("PhyMsgInf", Namespace = "urn:CBI:xsd:CBIBdySDDStsRpt.00.01.00")]
public PhyMsgInf PhyMsgInf { get; set; }
[XmlElement("CBIEnvelSDDStsRptLogMsg", Namespace = "urn:CBI:xsd:CBIBdySDDStsRpt.00.01.00")]
public CBIEnvelSDDStsRptLogMsg CBIEnvelSDDStsRptLogMsg { get; set; }
}
[Serializable]
public class CBIEnvelSDDStsRptLogMsg
{
[XmlElement("CBISDDStsRptLogMsg", Namespace = "urn:CBI:xsd:CBIBdySDDStsRpt.00.01.00")]
public CBISDDStsRptLogMsg CBISDDStsRptLogMsg { get; set; }
}
[Serializable]
public class CBISDDStsRptLogMsg
{
[XmlElement("GrpHdr", Namespace = "urn:CBI:xsd:CBISDDStsRptLogMsg.00.01.00")]
public GroupHeader GroupHeader { get; set; }
[XmlElement("OrgnlGrpInfAndSts", Namespace = "urn:CBI:xsd:CBISDDStsRptLogMsg.00.01.00")]
public OriginalGroupInformationAndStatus OriginalGroupInformationAndStatus { get; set; }
[XmlElement("OrgnlPmtInfAndSts", Namespace = "urn:CBI:xsd:CBISDDStsRptLogMsg.00.01.00")]
public List<OriginalPaymentInformationAndStatus> OriginalPaymentInformationAndStatus { get; set; }
}
My deserialization is pretty simple:
var sITA = new XmlSerializer(typeof(CBISDDStsRptPhyMsg));
var xmlITA = new CBISDDStsRptPhyMsg();
using(var reader = XmlReader.Create(fileInput.InputStream))
{
xmlITA = (CBISDDStsRptPhyMsg)sITA.Deserialize(reader);
}
and at the end of it, all of my 3 main objects are null.
Does anyone have a clue on what I'm doing wrong? I've already managed to serialize/deserialize every SEPA files (pain.00x) that I've found but this Italian standard is giving me quite some head-aches for a while.
Does anyone have the classes that are able to deserialize this italian standard? (Don't bother on finding the .xsd to generate it because I've already googled quite a bit and the .xsd is nowhere to be found!)
Thanks in advance.
The three child elements of the root element in the same namespace as the root, the one prefixed RISPOSTASDD:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<RISPOSTASDD:CBISDDStsRptPhyMsg xmlns:HE2E="urn:CBI:xsd:CBIHdrSrv.001.07" xmlns:BODY="urn:CBI:xsd:CBIBdySDDStsRpt.00.01.00" xmlns:SGNT="urn:CBI:xsd:CBISgnInf.001.04" xmlns:RISPOSTASDD="urn:CBI:xsd:CBISDDStsRptPhyMsg.00.01.00" xmlns:LMSG="urn:CBI:xsd:CBISDDStsRptLogMsg.00.01.00" xmlns:HTRT="urn:CBI:xsd:CBIHdrTrt.001.07">
<RISPOSTASDD:CBIHdrTrt>
<!-- Nested elements snipped -->
</RISPOSTASDD:CBIHdrTrt>
<RISPOSTASDD:CBIHdrSrv>
<!-- Nested elements snipped -->
</RISPOSTASDD:CBIHdrSrv>
<RISPOSTASDD:CBIBdySDDStsRpt>
<!-- Remaining XML not included in the question -->
Thus your root class needs to be modified as follows:
[Serializable, XmlRoot(Namespace = "urn:CBI:xsd:CBISDDStsRptPhyMsg.00.01.00")]
public class CBISDDStsRptPhyMsg
{
[XmlElement("CBIHdrTrt", Namespace = "urn:CBI:xsd:CBISDDStsRptPhyMsg.00.01.00")]
public CBIHdrTrt CBIHdrTrt { get; set; }
[XmlElement("CBIHdrSrv", Namespace = "urn:CBI:xsd:CBISDDStsRptPhyMsg.00.01.00")]
public CBIHdrSrv CBIHdrSrv { get; set; }
[XmlElement("CBIBdySDDStsRpt", Namespace = "urn:CBI:xsd:CBISDDStsRptPhyMsg.00.01.00")]
public CBIBdySDDStsRpt CBIBdySDDStsRpt { get; set; }
}
(Or you could just omit the Namespace = on the properties since it's the same as in the XmlRoot attribute.)
There may be other problems, but your question doesn't contain a full mcve (the XML and classes are both incomplete) but at the minimum this looks incorrect.

How do I deserialise this JSON?

I haven't done anything with JSON before... so I am probably just missing a step.
Here is an example of the JSON I want to deserialise:
{"item":{"icon":"http://services.runescape.com/m=itemdb_rs/4765_obj_sprite.gif?id=4798","icon_large":"http://services.runescape.com/m=itemdb_rs/4765_obj_big.gif?id=4798","id":4798,"type":"Ammo","typeIcon":"http://www.runescape.com/img/categories/Ammo","name":"Adamant brutal","description":"Blunt adamantite arrow...ouch","current":{"trend":"neutral","price":237},"today":{"trend":"neutral","price":0},"members":"true","day30":{"trend":"positive","change":"+1.0%"},"day90":{"trend":"negative","change":"-0.0%"},"day180":{"trend":"positive","change":"+0.0%"}}}
I put this into "Json 2 C#" and ended up creating this new .cs file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RSTool.Models
{
public class Current
{
public string trend { get; set; }
public int price { get; set; }
}
public class Today
{
public string trend { get; set; }
public int price { get; set; }
}
public class Day30
{
public string trend { get; set; }
public string change { get; set; }
}
public class Day90
{
public string trend { get; set; }
public string change { get; set; }
}
public class Day180
{
public string trend { get; set; }
public string change { get; set; }
}
public class Item
{
public string icon { get; set; }
public string icon_large { get; set; }
public int id { get; set; }
public string type { get; set; }
public string typeIcon { get; set; }
public string name { get; set; }
public string description { get; set; }
public Current current { get; set; }
public Today today { get; set; }
public string members { get; set; }
public Day30 day30 { get; set; }
public Day90 day90 { get; set; }
public Day180 day180 { get; set; }
}
public class RootObject
{
public Item item { get; set; }
}
}
So, I have the class. I can retrieve the JSON from its location as a string, but I really have no idea how to deserialise it... I have installed Newtonsoft.Json and have tried using PopulateObject and also Deserializer but not with any luck...
Assuming that my JSON is stored as a string called "json", how would I go about storing that query and then retrieving the item name, for example?
Use:
var deserialized = JsonConvert.DeserializeObject<RootObject>(json);
I just tested this successfully given the code you supplied.
You can then access properties of object as normal:
MessageBox.Show(deserialized.item.name);

Categories