我正在尝试将此xml字符串响应反序列化为类:
<?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>
这是我尝试将字符串反序列化为:
的类 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; }
}
}
以下是抛出错误的代码:
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;
}
抛出的异常错误是:{"<result xmlns=''> was not expected."}
当涉及到xml时,我总是有很多麻烦反序列化...我知道这里有很多我不知道的。帮助
答案 0 :(得分:1)
您好,您应该反序列化以键入Result而不是TResponse,如下所示
TextReader sr = new StringReader(response);
var deserializer = new XmlSerializer(typeof(Result));
Object storage = (Result)deserializer.Deserialize(sr);
您的存储变量现在将填充对象
答案 1 :(得分:1)
试试这个
XmlSerializer serializer = new XmlSerializer(typeof(DOTWSearchResponse.Result));
StringReader reader = new StringReader(response);
DOTWSearchResponse.Result deserialized = (DOTWSearchResponse.Result)serializer.Deserialize(reader);