Reputation: 1622
I'm trying to deserialize this xml string response to a class:
<?xml version="1.0" encoding="UTF-8"?>
<result command="searchhotels" tID="1463758732000001" ip="70.112.14.31" date="2016-05-20 15:39:00" version="2.0" elapsedTime="8.5034000873566">
<currencyShort>USD</currencyShort>
<hotels count="6">
<hotel runno="0" preferred="no" cityname="DUBAI" order="3" hotelid="856915">
<from>5549.2769
<formatted>5,549.28</formatted>
</from>
<fromMinimumSelling>6975
<formatted>6,975.00</formatted>
</fromMinimumSelling>
<availability>available</availability>
</hotel>
<hotel runno="1" preferred="no" cityname="DUBAI" order="3" hotelid="30924">
<from>5819.9734
<formatted>5,819.97</formatted>
</from>
<fromMinimumSelling>7316
<formatted>7,316.00</formatted>
</fromMinimumSelling>
<availability>available</availability>
</hotel>
<hotel runno="2" preferred="no" cityname="DUBAI" order="3" hotelid="994075">
<from>5459.0448
<formatted>5,459.04</formatted>
</from>
<fromMinimumSelling>6851
<formatted>6,851.00</formatted>
</fromMinimumSelling>
<availability>available</availability>
</hotel>
<hotel runno="3" preferred="no" cityname="DUBAI" order="3" hotelid="911115">
<from>6418
<formatted>6,418.00</formatted>
</from>
<fromMinimumSelling>7976
<formatted>7,976.00</formatted>
</fromMinimumSelling>
<availability>available</availability>
</hotel>
<hotel runno="4" preferred="no" cityname="DUBAI" order="3" hotelid="994005">
<from>4821.246
<formatted>4,821.25</formatted>
</from>
<fromMinimumSelling>6107
<formatted>6,107.00</formatted>
</fromMinimumSelling>
<availability>available</availability>
</hotel>
<hotel runno="5" preferred="no" cityname="DUBAI" order="3" hotelid="446025">
<from>11173.763
<formatted>11,173.76</formatted>
</from>
<fromMinimumSelling>13864
<formatted>13,864.00</formatted>
</fromMinimumSelling>
<availability>available</availability>
</hotel>
</hotels>
<successful>TRUE</successful>
</result>
Here is the class im trying to deserialize the string to:
public class DOTWSearchResponse
{
[XmlRoot(ElementName = "from")]
public class From
{
[XmlElement(ElementName = "formatted")]
public string Formatted { get; set; }
}
[XmlRoot(ElementName = "fromMinimumSelling")]
public class FromMinimumSelling
{
[XmlElement(ElementName = "formatted")]
public string Formatted { get; set; }
}
[XmlRoot(ElementName = "hotel")]
public class Hotel
{
[XmlElement(ElementName = "from")]
public From From { get; set; }
[XmlElement(ElementName = "fromMinimumSelling")]
public FromMinimumSelling FromMinimumSelling { get; set; }
[XmlElement(ElementName = "availability")]
public string Availability { get; set; }
[XmlAttribute(AttributeName = "runno")]
public string Runno { get; set; }
[XmlAttribute(AttributeName = "preferred")]
public string Preferred { get; set; }
[XmlAttribute(AttributeName = "cityname")]
public string Cityname { get; set; }
[XmlAttribute(AttributeName = "order")]
public string Order { get; set; }
[XmlAttribute(AttributeName = "hotelid")]
public string Hotelid { get; set; }
}
[XmlRoot(ElementName = "hotels")]
public class Hotels
{
[XmlElement(ElementName = "hotel")]
public List<Hotel> Hotel { get; set; }
[XmlAttribute(AttributeName = "count")]
public string Count { get; set; }
}
[XmlRoot(ElementName = "result")]
public class Result
{
[XmlElement(ElementName = "currencyShort")]
public string CurrencyShort { get; set; }
[XmlElement(ElementName = "hotels")]
public Hotels Hotels { get; set; }
[XmlElement(ElementName = "successful")]
public string Successful { get; set; }
[XmlAttribute(AttributeName = "command")]
public string Command { get; set; }
[XmlAttribute(AttributeName = "tID")]
public string TID { get; set; }
[XmlAttribute(AttributeName = "ip")]
public string Ip { get; set; }
[XmlAttribute(AttributeName = "date")]
public string Date { get; set; }
[XmlAttribute(AttributeName = "version")]
public string Version { get; set; }
[XmlAttribute(AttributeName = "elapsedTime")]
public string ElapsedTime { get; set; }
}
}
Here is the code that is throwing the error:
try
{
XmlSerializer serializer = new XmlSerializer(typeof(TResponse));
StringReader reader = new StringReader(response);
TResponse deserialized = (TResponse)serializer.Deserialize(reader);
return deserialized;
}
catch (Exception exception)
{
throw exception;
}
And the exception error thrown is: {"<result xmlns=''> was not expected."}
I always have alot of trouble deserializing when it comes to xml...I know there is lots I'm missing here. Help?
Upvotes: 0
Views: 49
Reputation: 34421
Try this
XmlSerializer serializer = new XmlSerializer(typeof(DOTWSearchResponse.Result));
StringReader reader = new StringReader(response);
DOTWSearchResponse.Result deserialized = (DOTWSearchResponse.Result)serializer.Deserialize(reader);
Upvotes: 1
Reputation: 774
Hi you should deserialize to type Result instead of TResponse something like below
TextReader sr = new StringReader(response);
var deserializer = new XmlSerializer(typeof(Result));
Object storage = (Result)deserializer.Deserialize(sr);
your Storage variable will now have objects being populated
Upvotes: 1