I am retrieving xml data from an web api and deserializing the data into objects.:
<Result>
<VendorInfo xml:lang="xx">
<Vendor vname="A" cpe="B">
<Product pname="C" cpe="D"/>
</Vendor>
<Vendor vname="E" cpe="F">
<Product pname="G" cpe="H"/>
</Vendor>
<Vendor vname="I" cpe="J">
<Product pname="K" cpe="L"/>
<Product pname="M" cpe="N"/>
</Vendor>
</VendorInfo>
<Status keyword="hoge" feed="bar"/>
</Result>
My current code is this:
[XmlRoot(ElementName = "Product")]
public class Product
{
[XmlAttribute(AttributeName = "pname")]
public string Pname { get; set; }
[XmlAttribute(AttributeName = "cpe")]
public string Cpe { get; set; }
}
[XmlRoot(ElementName = "Vendor")]
public class Vendor
{
[XmlArrayItem(ElementName = "Product")]
public List<Product> Product { get; set; }
[XmlAttribute(AttributeName = "vname")]
public string Vname { get; set; }
[XmlAttribute(AttributeName = "cpe")]
public string Cpe { get; set; }
}
[XmlRoot(ElementName = "VendorInfo")]
public class VendorInfo
{
[XmlElement(ElementName = "Vendor")]
public List<Vendor> Vendor { get; set; }
[XmlAttribute(AttributeName = "lang")]
public string Lang { get; set; }
}
[XmlRoot(ElementName = "Status")]
public class Status
{
[XmlAttribute(AttributeName = "feed")]
public string Feed { get; set; }
[XmlAttribute(AttributeName = "keyword")]
public string Keyword { get; set; }
}
[XmlRoot(ElementName = "Result")]
public class Result
{
[XmlElement(ElementName = "VendorInfo")]
public VendorInfo VendorInfo { get; set; }
[XmlElement(ElementName = "Status")]
public Status Status { get; set; }
}
But, This code does not working correctly.
Only first 2 Vendor elements are deserialized, Product element is not deserialized.
What am i doing wrong?
best regards
You have few things to be taken care.
Make only one element XmlRoot. Refer below link and search for "Controlling Serialization of Classes Using XmlRootAttribute and XmlTypeAttribute".
https://learn.microsoft.com/en-us/dotnet/standard/serialization/controlling-xml-serialization-using-attributes
Here in your case, the "Result" class is root.
Make rest all XmlType.
In the vendor class instead of "XmlArrayItem", make it just "XmlElement".
Here is the working code.
[XmlType("Product")]
public class Product
{
[XmlAttribute(AttributeName = "pname")]
public string Pname { get; set; }
[XmlAttribute(AttributeName = "cpe")]
public string Cpe { get; set; }
}
[XmlType("Vendor")]
public class Vendor
{
[XmlElement(ElementName = "Product")]
public List<Product> Product { get; set; }
[XmlAttribute(AttributeName = "vname")]
public string Vname { get; set; }
[XmlAttribute(AttributeName = "cpe")]
public string Cpe { get; set; }
}
[XmlType("VendorInfo")]
public class VendorInfo
{
[XmlElement(ElementName = "Vendor")]
public List<Vendor> Vendor { get; set; }
[XmlAttribute(AttributeName = "xml:lang")]
public string Lang { get; set; }
}
[XmlType("Status")]
public class Status
{
[XmlAttribute(AttributeName = "feed")]
public string Feed { get; set; }
[XmlAttribute(AttributeName = "keyword")]
public string Keyword { get; set; }
}
[XmlRoot(ElementName = "Result")]
public class Result
{
[XmlElement(ElementName = "VendorInfo")]
public VendorInfo VendorInfo { get; set; }
[XmlElement(ElementName = "Status")]
public Status Status { get; set; }
}
Related
Trying to Deserialise XML response to C#. but getting error as specified below.
My code:
IRestResponse result = Client.Execute<StudentPersonal>(new RestRequest(Method.POST)
.AddHeader("Accept", " application/xml")
.AddHeader("Authorization", "Basic Y2xaTWNqZFFlbFF4YVU1TlFtUnZOV3hXTkhadmNHaFhPSGxTT0Rka1VIcFhhRTl5TWtWQ05rRjNUMlp5VlUxWlNYa3hjbEU1V1RGUmMxTkNOMlZWVlRvNk9nPT06eUkxdERIanNVcGxLUUpzUlNXNTVacXpRVUFGQ3JrZzFwUGxUbDhUTmRQaFU0Z0xsTFJlQkVxTmhzZml3TnpaVA==")
.AddHeader("Content-Type", "application/xml")
.AddParameter("application/xml", body, ParameterType.RequestBody));
StudentPersonal firstResponse = new StudentPersonal();
StringReader srt = new StringReader(result.Content);
XmlSerializer serializer = new XmlSerializer(typeof(StudentPersonal), new XmlRootAttribute("StudentPersonal"));
firstResponse = (StudentPersonal)serializer.Deserialize(srt);
Console.WriteLine(firstResponse);
Assert.AreEqual("Created", result.StatusCode.ToString());
My classes:
[XmlRoot(ElementName = "StudentPersonal")]
public class StudentPersonal
{
//// XmlSerializer serializer = new XmlSerializer(typeof(StudentPersonal));
//// using (StringReader reader = new StringReader(xml))
//// {
//// var test = (StudentPersonal)serializer.Deserialize(reader);
//// }
[XmlElement(ElementName = "LocalId")]
public double LocalId { get; set; }
[XmlElement(ElementName = "StateProvinceId")]
public int StateProvinceId { get; set; }
[XmlElement(ElementName = "ElectronicIdList")]
public object ElectronicIdList { get; set; }
[XmlElement(ElementName = "OtherIdList")]
public OtherIdList OtherIdList { get; set; }
[XmlElement(ElementName = "PersonInfo")]
public PersonInfo PersonInfo { get; set; }
[XmlElement(ElementName = "GiftedTalented")]
public string GiftedTalented { get; set; }
[XmlElement(ElementName = "EconomicDisadvantage")]
public string EconomicDisadvantage { get; set; }
[XmlElement(ElementName = "ESL")]
public string ESL { get; set; }
[XmlElement(ElementName = "YoungCarersRole")]
public string YoungCarersRole { get; set; }
[XmlElement(ElementName = "Disability")]
public string Disability { get; set; }
[XmlElement(ElementName = "IntegrationAide")]
public string IntegrationAide { get; set; }
[XmlElement(ElementName = "EducationSupport")]
public string EducationSupport { get; set; }
[XmlElement(ElementName = "Sensitive")]
public string Sensitive { get; set; }
[XmlAttribute(AttributeName = "xsd")]
public string Xsd { get; set; }
[XmlAttribute(AttributeName = "xsi")]
public string Xsi { get; set; }
[XmlAttribute(AttributeName = "RefId")]
public string RefId { get; set; }
[XmlAttribute(AttributeName = "xmlns")]
public string Xmlns { get; set; }
[XmlText]
public string Text { get; set; }
}
[XmlRoot(ElementName = "OtherId")]
public class OtherId
{
[XmlAttribute(AttributeName = "Type")]
public string Type { get; set; }
[XmlText]
public int Text { get; set; }
}
[XmlRoot(ElementName = "OtherIdList")]
public class OtherIdList
{
[XmlElement(ElementName = "OtherId")]
public List<OtherId> OtherId { get; set; }
}
[XmlRoot(ElementName = "Name")]
public class Name
{
[XmlElement(ElementName = "Title")]
public string Title { get; set; }
[XmlElement(ElementName = "FamilyName")]
public string FamilyName { get; set; }
[XmlElement(ElementName = "GivenName")]
public string GivenName { get; set; }
[XmlElement(ElementName = "MiddleName")]
public string MiddleName { get; set; }
[XmlElement(ElementName = "FamilyNameFirst")]
public string FamilyNameFirst { get; set; }
[XmlElement(ElementName = "PreferredFamilyName")]
public string PreferredFamilyName { get; set; }
[XmlElement(ElementName = "PreferredFamilyNameFirst")]
public string PreferredFamilyNameFirst { get; set; }
[XmlElement(ElementName = "PreferredGivenName")]
public string PreferredGivenName { get; set; }
[XmlElement(ElementName = "FullName")]
public string FullName { get; set; }
[XmlAttribute(AttributeName = "Type")]
public string Type { get; set; }
[XmlText]
public string Text { get; set; }
}
[XmlRoot(ElementName = "EnglishProficiency")]
public class EnglishProficiency
{
[XmlElement(ElementName = "Code")]
public int Code { get; set; }
}
[XmlRoot(ElementName = "Language")]
public class Language
{
[XmlElement(ElementName = "Code")]
public int Code { get; set; }
[XmlElement(ElementName = "LanguageType")]
public int LanguageType { get; set; }
}
[XmlRoot(ElementName = "LanguageList")]
public class LanguageList
{
[XmlElement(ElementName = "Language")]
public List<Language> Language { get; set; }
}
[XmlRoot(ElementName = "DwellingArrangement")]
public class DwellingArrangement
{
[XmlElement(ElementName = "Code")]
public int Code { get; set; }
}
[XmlRoot(ElementName = "Religion")]
public class Religion
{
[XmlElement(ElementName = "Code")]
public int Code { get; set; }
}
[XmlRoot(ElementName = "ReligiousEvent")]
public class ReligiousEvent
{
[XmlElement(ElementName = "Type")]
public string Type { get; set; }
[XmlElement(ElementName = "Date")]
public DateTime Date { get; set; }
}
[XmlRoot(ElementName = "ReligiousEventList")]
public class ReligiousEventList
{
[XmlElement(ElementName = "ReligiousEvent")]
public ReligiousEvent ReligiousEvent { get; set; }
}
[XmlRoot(ElementName = "Demographics")]
public class Demographics
{
[XmlElement(ElementName = "IndigenousStatus")]
public int IndigenousStatus { get; set; }
[XmlElement(ElementName = "Sex")]
public int Sex { get; set; }
[XmlElement(ElementName = "BirthDate")]
public DateTime BirthDate { get; set; }
[XmlElement(ElementName = "BirthDateVerification")]
public int BirthDateVerification { get; set; }
[XmlElement(ElementName = "CountryArrivalDate")]
public DateTime CountryArrivalDate { get; set; }
[XmlElement(ElementName = "EnglishProficiency")]
public EnglishProficiency EnglishProficiency { get; set; }
[XmlElement(ElementName = "LanguageList")]
public LanguageList LanguageList { get; set; }
[XmlElement(ElementName = "DwellingArrangement")]
public DwellingArrangement DwellingArrangement { get; set; }
[XmlElement(ElementName = "Religion")]
public Religion Religion { get; set; }
[XmlElement(ElementName = "ReligiousEventList")]
public ReligiousEventList ReligiousEventList { get; set; }
[XmlElement(ElementName = "PermanentResident")]
public string PermanentResident { get; set; }
[XmlElement(ElementName = "VisaSubClass")]
public int VisaSubClass { get; set; }
[XmlElement(ElementName = "VisaStatisticalCode")]
public string VisaStatisticalCode { get; set; }
[XmlElement(ElementName = "VisaExpiryDate")]
public DateTime VisaExpiryDate { get; set; }
[XmlElement(ElementName = "LBOTE")]
public string LBOTE { get; set; }
[XmlElement(ElementName = "ImmunisationCertificateStatus")]
public string ImmunisationCertificateStatus { get; set; }
[XmlElement(ElementName = "CulturalBackground")]
public int CulturalBackground { get; set; }
[XmlElement(ElementName = "MaritalStatus")]
public int MaritalStatus { get; set; }
[XmlElement(ElementName = "MedicareNumber")]
public int MedicareNumber { get; set; }
}
[XmlRoot(ElementName = "PhoneNumber")]
public class PhoneNumber
{
[XmlElement(ElementName = "Number")]
public int Number { get; set; }
[XmlElement(ElementName = "ListedStatus")]
public string ListedStatus { get; set; }
[XmlAttribute(AttributeName = "Type")]
public int Type { get; set; }
[XmlText]
public string Text { get; set; }
}
[XmlRoot(ElementName = "PhoneNumberList")]
public class PhoneNumberList
{
[XmlElement(ElementName = "PhoneNumber")]
public PhoneNumber PhoneNumber { get; set; }
}
[XmlRoot(ElementName = "Email")]
public class Email
{
[XmlAttribute(AttributeName = "Type")]
public int Type { get; set; }
[XmlText]
public string Text { get; set; }
}
[XmlRoot(ElementName = "EmailList")]
public class EmailList
{
[XmlElement(ElementName = "Email")]
public List<Email> Email { get; set; }
}
[XmlRoot(ElementName = "PersonInfo")]
public class PersonInfo
{
[XmlElement(ElementName = "Name")]
public Name Name { get; set; }
[XmlElement(ElementName = "Demographics")]
public Demographics Demographics { get; set; }
[XmlElement(ElementName = "AddressList")]
public object AddressList { get; set; }
[XmlElement(ElementName = "PhoneNumberList")]
public PhoneNumberList PhoneNumberList { get; set; }
[XmlElement(ElementName = "EmailList")]
public EmailList EmailList { get; set; }
}
Below is the response XML details from Result.content:
<StudentPersonal xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" RefId=\"9ec476fd-09b5-4b2b-967a-fb80b207526e\" xmlns=\"http://www.sifassociation.org/datamodel/au/3.4\">
<LocalId>2432717276450097998</LocalId>
<StateProvinceId>674361</StateProvinceId>
<ElectronicIdList />
<OtherIdList>
<OtherId Type=\"MazeKey\">674363</OtherId>
<OtherId Type=\"VETUSI\">674362</OtherId>
<OtherId Type=\"UPN\">674363</OtherId>
</OtherIdList>
<PersonInfo>
<Name Type=\"LGL\">
<Title>Ms</Title>
<FamilyName>Gutteridge2</FamilyName>
<GivenName>Ferdinanda2</GivenName>
<MiddleName>Creamer1</MiddleName>
<FamilyNameFirst>N</FamilyNameFirst>
<PreferredFamilyName>Gutteridge</PreferredFamilyName>
<PreferredFamilyNameFirst>N</PreferredFamilyNameFirst>
<PreferredGivenName>Ferdinanda</PreferredGivenName>
<FullName> Ferdinanda Gutteridge</FullName>
</Name>
<Demographics>
<IndigenousStatus>1</IndigenousStatus>
<Sex>2</Sex>
<BirthDate>2006-03-21</BirthDate>
<BirthDateVerification>1004</BirthDateVerification>
<CountryArrivalDate>2008-09-17</CountryArrivalDate>
<EnglishProficiency>
<Code>1</Code>
</EnglishProficiency>
<LanguageList>
<Language>
<Code>1201</Code>
<LanguageType>3</LanguageType>
</Language>
<Language>
<Code>4303</Code>
<LanguageType>1</LanguageType>
</Language>
<Language>
<Code>4303</Code>
<LanguageType>1</LanguageType>
</Language>
</LanguageList>
<DwellingArrangement>
<Code>1671</Code>
</DwellingArrangement>
<Religion>
<Code>0002</Code>
</Religion>
<ReligiousEventList>
<ReligiousEvent>
<Type>Confirmation</Type>
<Date>2018-07-17</Date>
</ReligiousEvent>
</ReligiousEventList>
<PermanentResident>P</PermanentResident>
<VisaSubClass>124</VisaSubClass>
<VisaStatisticalCode>VisaStatisticalCode</VisaStatisticalCode>
<VisaExpiryDate>2019-01-17</VisaExpiryDate>
<LBOTE>N</LBOTE>
<ImmunisationCertificateStatus>C</ImmunisationCertificateStatus>
<CulturalBackground>0901</CulturalBackground>
<MaritalStatus>3</MaritalStatus>
<MedicareNumber>67436</MedicareNumber>
</Demographics>
<AddressList />
<PhoneNumberList>
<PhoneNumber Type=\"0350\">
<Number>12367436</Number>
<ListedStatus>U</ListedStatus>
</PhoneNumber>
</PhoneNumberList>
<EmailList>
<Email Type=\"01\">person67436#email1.com.au</Email>
<Email Type=\"02\">person67436#email2.com.au</Email>
<Email Type=\"03\">person67436#email3.com.au</Email>
</EmailList>
</PersonInfo>
<GiftedTalented>N</GiftedTalented>
<EconomicDisadvantage>N</EconomicDisadvantage>
<ESL>N</ESL>
<YoungCarersRole>N</YoungCarersRole>
<Disability>N</Disability>
<IntegrationAide>N</IntegrationAide>
<EducationSupport>U</EducationSupport>
<Sensitive>N</Sensitive>
</StudentPersonal>
Error is from the above XML response. Getting Error
There is an error in XML document (1, 2). InvalidOperationException: <StudentPersonal xmlns='http://www.sifassociation.org/datamodel/au/3.4'> was not expected.
There seems to be two issues.
Add the namespace to your class:
[XmlRoot(ElementName = "StudentPersonal", Namespace = "http://www.sifassociation.org/datamodel/au/3.4")]
public class StudentPersonal
...
In the XML, all " are escaped with \.
<StudentPersonal xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" RefId=\"9ec476fd-09b5-4b2b-967a-fb80b207526e\" xmlns=\"http://www.sifassociation.org/datamodel/au/3.4\">
...
</StudentPersonal>
This also occurs in other places throughout the XML. Such as:
<PhoneNumber Type=\"0350\">
To resolve the issue, replace all occurrences of \" with ".
<StudentPersonal xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" RefId="9ec476fd-09b5-4b2b-967a-fb80b207526e" xmlns="http://www.sifassociation.org/datamodel/au/3.4">
...
</StudentPersonal>
and
<PhoneNumber Type="0350">
Here's a method that can be used to deserialize the XML:
DeserializeXMLStringToObject:
public static T DeserializeXMLStringToObject<T>(string xmlData)
{
T rObject = default(T);
try
{
if (string.IsNullOrEmpty(xmlData))
{
return default(T);
}
//replace \" with "
string xmlDataSanitized = xmlData.Replace("\\\"", "\"");
using (System.IO.StringReader reader = new System.IO.StringReader(xmlDataSanitized))
{
//add the namespace to the class instead of adding it here
//System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(T), "http://www.sifassociation.org/datamodel/au/3.4");
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(T));
rObject = (T)serializer.Deserialize(reader);
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
throw ex;
}
return rObject;
}
Usage:
private StudentPersonal _studentPersonal = null;
...
_studentPersonal = DeserializeXMLStringToObject<StudentPersonal>(result.Content);
//for testing, display some of the data
//System.Diagnostics.Debug.WriteLine("LocalId: " + _studentPersonal.LocalId);
//System.Diagnostics.Debug.WriteLine("GivenName: " + _studentPersonal.PersonInfo.Name.GivenName);
I am making a web API for a application. Except for all that I had made to the web API, I want to make an HTTP POST request to a link that will send me a response in XML. I had to send a request every 5 minutes in order to take the last values of the products. These values of the XML I want to put it in a database I order to have access to it. The database I will use is MySql and I make the API in C#. I had made the model of the XML through the http://xmltocsharp.azurewebsites.net/:
[XmlRoot(ElementName = "dd")]
public class Dd
{
[XmlElement(ElementName = "code")]
public string Code { get; set; }
[XmlElement(ElementName = "dd_descr")]
public string Dd_descr { get; set; }
[XmlElement(ElementName = "dimos_descr")]
public string Dimos_descr { get; set; }
[XmlElement(ElementName = "nomos_descr")]
public string Nomos_descr { get; set; }
}
[XmlRoot(ElementName = "address")]
public class Address
{
[XmlElement(ElementName = "fulladdress")]
public string Fulladdress { get; set; }
[XmlElement(ElementName = "zipcode")]
public string Zipcode { get; set; }
[XmlElement(ElementName = "dd")]
public Dd Dd { get; set; }
}
[XmlRoot(ElementName = "product")]
public class Product
{
[XmlElement(ElementName = "code")]
public string Code { get; set; }
[XmlElement(ElementName = "description")]
public string Description { get; set; }
}
[XmlRoot(ElementName = "company")]
public class Company
{
[XmlElement(ElementName = "code")]
public string Code { get; set; }
[XmlElement(ElementName = "name")]
public string Name { get; set; }
}
[XmlRoot(ElementName = "priceentry")]
public class Priceentry
{
[XmlElement(ElementName = "station")]
public string Station { get; set; }
[XmlElement(ElementName = "name")]
public string Name { get; set; }
[XmlElement(ElementName = "address")]
public Address Address { get; set; }
[XmlElement(ElementName = "product")]
public Product Product { get; set; }
[XmlElement(ElementName = "price")]
public string Price { get; set; }
[XmlElement(ElementName = "timestamp")]
public string Timestamp { get; set; }
[XmlElement(ElementName = "company")]
public Company Company { get; set; }
}
[XmlRoot(ElementName = "pricelist")]
public class Pricelist
{
[XmlElement(ElementName = "priceentry")]
public List<Priceentry> Priceentry { get; set; }
}
}
A part of the xml is:
<pricelist>
<priceentry>
<station>2</station>
<name> name </name>
<address>
<fulladdress> name </fulladdress>
<zipcode>10442</zipcode>
<dd>
<code>A1010400</code>
<dd_descr> name
</dd_descr>
<dimos_descr> name </dimos_descr>
<nomos_descr> name </nomos_descr>
</dd>
</address>
<product>
<code>11</code>
<description> name </description>
</product>
<price>1.558</price>
<timestamp>1576649282267</timestamp>
<company>
<code>2</code>
<name> name </name>
</company>
</priceentry>
What I am asking is the following: what is the best way to accomplish what I have described
This is my xml:
<?xml version="1.0"?>
<orders>
<order>
<customer_id>5675757</customer_id>
<order_code>6456</order_code>
<products>
<product>
<product_item_code>577868</product_item_code>
<product_item_amount>22</product_item_amount>
</product>
</products>
</order>
</orders>
And this is my class:
[XmlRoot(ElementName = "orders")]
public class Orders
{
public List<OrderModel> OrdersList { get; set; }
}
[XmlRoot(ElementName = "order")]
public class OrderModel
{
[XmlElement(ElementName = "customer_id")]
public string CustomerId { get; set; }
[XmlElement(ElementName = "order_code")]
public string OrderCode { get; set; }
[XmlElement(ElementName = "products")]
public List<Product> products { get; set; }
}
[XmlRoot(ElementName = "product")]
public class Product
{
[XmlElement(ElementName = "product_item_code")]
public string product_item_code { get; set; }
[XmlElement(ElementName = "product_item_amount")]
public string product_item_amount { get; set; }
}
And this is my code:
using (FileStream fileStream = new FileStream(filePath, FileMode.Open))
{
var serializer = new XmlSerializer(typeof(Orders));
var orders = (Orders)serializer.Deserialize(fileStream);
}
But it returns 0 elements. I know I am missing something obvious but I don't know what.
Change you class Orders and OrderModel:
[XmlRoot(ElementName = "orders")]
public class Orders
{
[XmlElement(ElementName = "order")]
public List<OrderModel> OrdersList { get; set; }
}
[XmlRoot(ElementName = "order")]
public class OrderModel
{
[XmlElement(ElementName = "customer_id")]
public string CustomerId { get; set; }
[XmlElement(ElementName = "order_code")]
public string OrderCode { get; set; }
[XmlArray(ElementName = "products")]
[XmlArrayItem(ElementName = "product")]
public List<Product> products { get; set; }
}
Then it should work
Modify your classes as below and it will work.
[XmlRoot(ElementName = "orders")]
public class Orders
{
[XmlElement(ElementName = "order")]
public List<OrderModel> OrdersList { get; set; }
}
public class OrderModel
{
[XmlElement(ElementName = "customer_id")]
public string CustomerId { get; set; }
[XmlElement(ElementName = "order_code")]
public string OrderCode { get; set; }
[XmlArray(ElementName = "products")]
[XmlArrayItem(ElementName = "product")]
public List<Product> products { get; set; }
}
public class Product
{
[XmlElement(ElementName = "product_item_code")]
public string product_item_code { get; set; }
[XmlElement(ElementName = "product_item_amount")]
public string product_item_amount { get; set; }
}
I have an XML string resposne that looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<Response ResponseReference="REF_D_026_145-2601465218898798">
<ResponseDetails Language="en">
<SearchHotelPriceResponse>
<HotelDetails>
<Hotel HasExtraInfo="true" HasMap="true" HasPictures="true">
<City Code="LON">
<![CDATA[London]]>
</City>
<Item Code="IBI3">
<![CDATA[IBIS EXCEL]]>
</Item>
<LocationDetails>
<Location Code="G9">
<![CDATA[Outside Centre]]>
</Location>
<Location Code="01">
<![CDATA[Docklands]]>
</Location>
</LocationDetails>
<StarRating>3</StarRating>
<HotelRooms>
<HotelRoom Code="DB" NumberOfRooms="1"/>
</HotelRooms>
<RoomCategories>
<RoomCategory Id="001:IBI3:17082:S16700:22530:91760">
<Description>
<![CDATA[Standard Twin]]>
</Description>
<ItemPrice CommissionPercentage="0.00" Currency="USD">1870.00</ItemPrice>
<Confirmation Code="IM">
<![CDATA[AVAILABLE]]>
</Confirmation>
<Meals>
<Basis Code="B">
<![CDATA[Breakfast]]>
</Basis>
<Breakfast Code="F">
<![CDATA[Full]]>
</Breakfast>
</Meals>
<HotelRoomPrices>
<HotelRoom Code="DB">
<RoomPrice Gross="1870.00"/>
<PriceRanges>
<PriceRange>
<DateRange>
<FromDate>2016-07-30</FromDate>
<ToDate>2016-08-02</ToDate>
</DateRange>
<Price Gross="467.50" Nights="4"/>
</PriceRange>
</PriceRanges>
</HotelRoom>
</HotelRoomPrices>
<ChargeConditions>
<ChargeCondition Type="cancellation">
<Condition Charge="true" ChargeAmount="1870.00" Currency="USD" FromDay="0" ToDay="0"/>
<Condition Charge="true" ChargeAmount="467.50" Currency="USD" FromDay="1" ToDay="2"/>
<Condition Charge="false" FromDay="3"/>
</ChargeCondition>
<ChargeCondition Type="amendment">
<Condition Charge="false" FromDay="0"/>
</ChargeCondition>
<PassengerNameChange Allowable="true"/>
</ChargeConditions>
</RoomCategory>
<RoomCategory Id="001:IBI3:17082:S16700:22530:91737">
<Description>
<![CDATA[Standard Triple]]>
</Description>
<ItemPrice CommissionPercentage="0.00" Currency="USD">2804.00</ItemPrice>
<Confirmation Code="IM">
<![CDATA[AVAILABLE]]>
</Confirmation>
<Meals>
<Basis Code="B">
<![CDATA[Breakfast]]>
</Basis>
<Breakfast Code="F">
<![CDATA[Full]]>
</Breakfast>
</Meals>
<HotelRoomPrices>
<HotelRoom Code="DB">
<RoomPrice Gross="2804.00"/>
<PriceRanges>
<PriceRange>
<DateRange>
<FromDate>2016-07-30</FromDate>
<ToDate>2016-08-02</ToDate>
</DateRange>
<Price Gross="701.00" Nights="4"/>
</PriceRange>
</PriceRanges>
</HotelRoom>
</HotelRoomPrices>
<ChargeConditions>
<ChargeCondition Type="cancellation">
<Condition Charge="true" ChargeAmount="2804.00" Currency="USD" FromDay="0" ToDay="0"/>
<Condition Charge="true" ChargeAmount="701.00" Currency="USD" FromDay="1" ToDay="2"/>
<Condition Charge="false" FromDay="3"/>
</ChargeCondition>
<ChargeCondition Type="amendment">
<Condition Charge="false" FromDay="0"/>
</ChargeCondition>
<PassengerNameChange Allowable="true"/>
</ChargeConditions>
</RoomCategory>
</RoomCategories>
</Hotel>
</HotelDetails>
</SearchHotelPriceResponse>
</ResponseDetails>
</Response>
And I the class I'm tring to deserialize this response to looks like this:
using System.Collections.Generic;
using System.Xml.Serialization;
namespace example
{
[XmlRoot(ElementName = "Response")]
public class GTASearchResponse
{
[XmlElement(ElementName = "Response")]
public Response response { get; set; }
}
[XmlRoot(ElementName = "City")]
public class City
{
[XmlAttribute(AttributeName = "Code")]
public string Code { get; set; }
[XmlText]
public string Text { get; set; }
}
[XmlRoot(ElementName = "Item")]
public class Item
{
[XmlAttribute(AttributeName = "Code")]
public string Code { get; set; }
[XmlText]
public string Text { get; set; }
}
[XmlRoot(ElementName = "Location")]
public class Location
{
[XmlAttribute(AttributeName = "Code")]
public string Code { get; set; }
[XmlText]
public string Text { get; set; }
}
[XmlRoot(ElementName = "LocationDetails")]
public class LocationDetails
{
[XmlElement(ElementName = "Location")]
public List<Location> Location { get; set; }
}
[XmlRoot(ElementName = "HotelRoom")]
public class HotelRoom
{
[XmlAttribute(AttributeName = "Code")]
public string Code { get; set; }
[XmlAttribute(AttributeName = "NumberOfRooms")]
public string NumberOfRooms { get; set; }
[XmlElement(ElementName = "RoomPrice")]
public RoomPrice RoomPrice { get; set; }
[XmlElement(ElementName = "PriceRanges")]
public PriceRanges PriceRanges { get; set; }
}
[XmlRoot(ElementName = "HotelRooms")]
public class HotelRooms
{
[XmlElement(ElementName = "HotelRoom")]
public HotelRoom HotelRoom { get; set; }
}
[XmlRoot(ElementName = "ItemPrice")]
public class ItemPrice
{
[XmlAttribute(AttributeName = "CommissionPercentage")]
public string CommissionPercentage { get; set; }
[XmlAttribute(AttributeName = "Currency")]
public string Currency { get; set; }
[XmlText]
public string Text { get; set; }
}
[XmlRoot(ElementName = "Confirmation")]
public class Confirmation
{
[XmlAttribute(AttributeName = "Code")]
public string Code { get; set; }
[XmlText]
public string Text { get; set; }
}
[XmlRoot(ElementName = "Basis")]
public class Basis
{
[XmlAttribute(AttributeName = "Code")]
public string Code { get; set; }
[XmlText]
public string Text { get; set; }
}
[XmlRoot(ElementName = "Breakfast")]
public class Breakfast
{
[XmlAttribute(AttributeName = "Code")]
public string Code { get; set; }
[XmlText]
public string Text { get; set; }
}
[XmlRoot(ElementName = "Meals")]
public class Meals
{
[XmlElement(ElementName = "Basis")]
public Basis Basis { get; set; }
[XmlElement(ElementName = "Breakfast")]
public Breakfast Breakfast { get; set; }
}
[XmlRoot(ElementName = "RoomPrice")]
public class RoomPrice
{
[XmlAttribute(AttributeName = "Gross")]
public string Gross { get; set; }
}
[XmlRoot(ElementName = "DateRange")]
public class DateRange
{
[XmlElement(ElementName = "FromDate")]
public string FromDate { get; set; }
[XmlElement(ElementName = "ToDate")]
public string ToDate { get; set; }
}
[XmlRoot(ElementName = "Price")]
public class Price
{
[XmlAttribute(AttributeName = "Gross")]
public string Gross { get; set; }
[XmlAttribute(AttributeName = "Nights")]
public string Nights { get; set; }
}
[XmlRoot(ElementName = "PriceRange")]
public class PriceRange
{
[XmlElement(ElementName = "DateRange")]
public DateRange DateRange { get; set; }
[XmlElement(ElementName = "Price")]
public Price Price { get; set; }
}
[XmlRoot(ElementName = "PriceRanges")]
public class PriceRanges
{
[XmlElement(ElementName = "PriceRange")]
public PriceRange PriceRange { get; set; }
}
[XmlRoot(ElementName = "HotelRoomPrices")]
public class HotelRoomPrices
{
[XmlElement(ElementName = "HotelRoom")]
public HotelRoom HotelRoom { get; set; }
}
[XmlRoot(ElementName = "Condition")]
public class Condition
{
[XmlAttribute(AttributeName = "Charge")]
public string Charge { get; set; }
[XmlAttribute(AttributeName = "ChargeAmount")]
public string ChargeAmount { get; set; }
[XmlAttribute(AttributeName = "Currency")]
public string Currency { get; set; }
[XmlAttribute(AttributeName = "FromDay")]
public string FromDay { get; set; }
[XmlAttribute(AttributeName = "ToDay")]
public string ToDay { get; set; }
}
[XmlRoot(ElementName = "ChargeCondition")]
public class ChargeCondition
{
[XmlElement(ElementName = "Condition")]
public List<Condition> Condition { get; set; }
[XmlAttribute(AttributeName = "Type")]
public string Type { get; set; }
}
[XmlRoot(ElementName = "PassengerNameChange")]
public class PassengerNameChange
{
[XmlAttribute(AttributeName = "Allowable")]
public string Allowable { get; set; }
}
[XmlRoot(ElementName = "ChargeConditions")]
public class ChargeConditions
{
[XmlElement(ElementName = "ChargeCondition")]
public List<ChargeCondition> ChargeCondition { get; set; }
[XmlElement(ElementName = "PassengerNameChange")]
public PassengerNameChange PassengerNameChange { get; set; }
}
[XmlRoot(ElementName = "RoomCategory")]
public class RoomCategory
{
[XmlElement(ElementName = "Description")]
public string Description { get; set; }
[XmlElement(ElementName = "ItemPrice")]
public ItemPrice ItemPrice { get; set; }
[XmlElement(ElementName = "Confirmation")]
public Confirmation Confirmation { get; set; }
[XmlElement(ElementName = "Meals")]
public Meals Meals { get; set; }
[XmlElement(ElementName = "HotelRoomPrices")]
public HotelRoomPrices HotelRoomPrices { get; set; }
[XmlElement(ElementName = "ChargeConditions")]
public ChargeConditions ChargeConditions { get; set; }
[XmlAttribute(AttributeName = "Id")]
public string Id { get; set; }
}
[XmlRoot(ElementName = "RoomCategories")]
public class RoomCategories
{
[XmlElement(ElementName = "RoomCategory")]
public List<RoomCategory> RoomCategory { get; set; }
}
[XmlRoot(ElementName = "Hotel")]
public class Hotel
{
[XmlElement(ElementName = "City")]
public City City { get; set; }
[XmlElement(ElementName = "Item")]
public Item Item { get; set; }
[XmlElement(ElementName = "LocationDetails")]
public LocationDetails LocationDetails { get; set; }
[XmlElement(ElementName = "StarRating")]
public string StarRating { get; set; }
[XmlElement(ElementName = "HotelRooms")]
public HotelRooms HotelRooms { get; set; }
[XmlElement(ElementName = "RoomCategories")]
public RoomCategories RoomCategories { get; set; }
[XmlAttribute(AttributeName = "HasExtraInfo")]
public string HasExtraInfo { get; set; }
[XmlAttribute(AttributeName = "HasMap")]
public string HasMap { get; set; }
[XmlAttribute(AttributeName = "HasPictures")]
public string HasPictures { get; set; }
}
[XmlRoot(ElementName = "HotelDetails")]
public class HotelDetails
{
[XmlElement(ElementName = "Hotel")]
public Hotel Hotel { get; set; }
}
[XmlRoot(ElementName = "SearchHotelPriceResponse")]
public class SearchHotelPriceResponse
{
[XmlElement(ElementName = "HotelDetails")]
public HotelDetails HotelDetails { get; set; }
}
[XmlRoot(ElementName = "ResponseDetails")]
public class ResponseDetails
{
[XmlElement(ElementName = "SearchHotelPriceResponse")]
public SearchHotelPriceResponse SearchHotelPriceResponse { get; set; }
[XmlAttribute(AttributeName = "Language")]
public string Language { get; set; }
}
[XmlRoot(ElementName = "Response")]
public class Response
{
[XmlElement(ElementName = "ResponseDetails")]
public ResponseDetails ResponseDetails { get; set; }
[XmlAttribute(AttributeName = "ResponseReference")]
public string ResponseReference { get; set; }
}
}
unfortunately when I deserialize I get a null value for a child element of the response property, visual studio looks like this when I debug:
You can see if i put a watch on the variable answer then I only get back a null Response class. Help?
From the Xml Attributes on GTASearchResponse it looks like you are expecting a schema
<response><response> ...
because there is the root and the attribute response. I think if you just deserialize your content directly to "Response" type it might work
I am working with a third party XML API, that returns XML responses. With the API, I am trying to import orders. So, two scenario could occur when I import order with this API:
Success - e.g. xml response: http://pastebin.com/ZcvvYMX6
Failure - e.g. xml response: http://pastebin.com/iVjqMKAR
What I am working on is a wrapper for this API and I have a method called ImportOrders. Within this method, I am trying to deserialize the xml to an object so that I can return a common response.
For example; I will have a common response DTO class like this:
public class ImportOrderResponse
{
bool IsError { get; set; }
string ErrorMsg { get; set; }
string Result { get; set; }
}
and the ImportOrders method will return response conditionally like this:
if (apiResult.Contains("importFailures"))
{
// todo: deserialize
return new ImportOrderResponse()
{
IsError = true,
ErrorMsg = "todo - get failureMessage"
};
}
else
{
// todo: deserialize
return new ImportOrderResponse()
{
IsError = false,
Result = "todo - haven't worked out what i need to return"
};
}
Using this site http://xmltocsharp.azurewebsites.net I've generated the classes used for deserialization like this:
ImportSuccess.cs
[XmlRoot(ElementName = "import")]
public class ImportSuccess
{
[XmlAttribute(AttributeName = "type")]
public string Type { get; set; }
[XmlAttribute(AttributeName = "operation")]
public string Operation { get; set; }
[XmlAttribute(AttributeName = "entity")]
public string Entity { get; set; }
[XmlAttribute(AttributeName = "externalReference")]
public string ExternalReference { get; set; }
[XmlAttribute(AttributeName = "item")]
public string Item { get; set; }
[XmlAttribute(AttributeName = "queryTime")]
public string QueryTime { get; set; }
}
[XmlRoot(ElementName = "importSuccesses")]
public class ImportSuccesses
{
[XmlElement(ElementName = "import")]
public List<ImportSuccess> Import { get; set; }
}
[XmlRoot(ElementName = "importResult")]
public class ImportResult
{
[XmlElement(ElementName = "importSuccesses")]
public ImportSuccesses ImportSuccesses { get; set; }
[XmlElement(ElementName = "importFailures")]
public string ImportFailures { get; set; }
[XmlElement(ElementName = "importDuplicates")]
public string ImportDuplicates { get; set; }
}
ImportFailure.cs
[XmlRoot(ElementName = "import")]
public class ImportFailure
{
[XmlElement(ElementName = "failureMessage")]
public string FailureMessage { get; set; }
[XmlElement(ElementName = "failureDetail")]
public string FailureDetail { get; set; }
[XmlAttribute(AttributeName = "type")]
public string Type { get; set; }
[XmlAttribute(AttributeName = "operation")]
public string Operation { get; set; }
[XmlAttribute(AttributeName = "externalReference")]
public string ExternalReference { get; set; }
[XmlAttribute(AttributeName = "queryTime")]
public string QueryTime { get; set; }
}
[XmlRoot(ElementName = "importFailures")]
public class ImportFailures
{
[XmlElement(ElementName = "import")]
public ImportFailure Import { get; set; }
}
[XmlRoot(ElementName = "importResult")]
public class ImportResult
{
[XmlElement(ElementName = "importSuccesses")]
public string ImportSuccesses { get; set; }
[XmlElement(ElementName = "importFailures")]
public ImportFailures ImportFailures { get; set; }
[XmlElement(ElementName = "importDuplicates")]
public string ImportDuplicates { get; set; }
}
But now I have a problem. Both the success & failure response contains the node importResult but has varying fields/element names.
Now I have an error because I have a duplicate ImportResult class.
How can I solve this? What is the correct way to handle this scenario?
I believe I was being silly... I was going about this with a wrong approach.
What I did was;
Call api with correct data for a success response, took the xml and created a class
call api with wrong/bad data for a failure response, took the xml and tried to create another class
After some digging around, I realised that the response structure is very close for success/failure.
So, I called the api and instead of sending one order; I sent 3 orders for 3 specific scenario. This resulted in a single XML response like this:
http://pastebin.com/sNgWAwq1
Now I have a good structure of the XML for (Success/Failure & Duplicates). Using this XML, I've generated the following class (for deserialisation purpose):
[XmlRoot(ElementName = "import")]
public class Import
{
[XmlAttribute(AttributeName = "type")]
public string Type { get; set; }
[XmlAttribute(AttributeName = "operation")]
public string Operation { get; set; }
[XmlAttribute(AttributeName = "entity")]
public string Entity { get; set; }
[XmlAttribute(AttributeName = "externalReference")]
public string ExternalReference { get; set; }
[XmlAttribute(AttributeName = "item")]
public string Item { get; set; }
[XmlAttribute(AttributeName = "queryTime")]
public string QueryTime { get; set; }
[XmlElement(ElementName = "failureMessage")]
public string FailureMessage { get; set; }
[XmlElement(ElementName = "failureDetail")]
public string FailureDetail { get; set; }
[XmlElement(ElementName = "duplicateMessage")]
public string DuplicateMessage { get; set; }
[XmlElement(ElementName = "duplicateDetail")]
public string DuplicateDetail { get; set; }
}
[XmlRoot(ElementName = "importSuccesses")]
public class ImportSuccesses
{
[XmlElement(ElementName = "import")]
public List<Import> Import { get; set; }
}
[XmlRoot(ElementName = "importFailures")]
public class ImportFailures
{
[XmlElement(ElementName = "import")]
public Import Import { get; set; }
}
[XmlRoot(ElementName = "importDuplicates")]
public class ImportDuplicates
{
[XmlElement(ElementName = "import")]
public Import Import { get; set; }
}
[XmlRoot(ElementName = "importResult")]
public class OrderImportResponse
{
[XmlElement(ElementName = "importSuccesses")]
public ImportSuccesses ImportSuccesses { get; set; }
[XmlElement(ElementName = "importFailures")]
public ImportFailures ImportFailures { get; set; }
[XmlElement(ElementName = "importDuplicates")]
public ImportDuplicates ImportDuplicates { get; set; }
}
Now, this can work for me. I can call the api and deserialise the response to the OrderImportResponse class (without checking what kind of xml response it was) and it will have all three scenario responses I am looking for.