XML反序列化为类错误 [英] Xml Deserialization to Class Error

查看:248
本文介绍了XML反序列化为类错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试反序列化该xml字符串对类的响应:

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;
        }

抛出的异常错误为: {< ; result xmlns =''>出乎意料。} 当涉及到xml时,我总是遇到很多反序列化的麻烦...我知道这里有很多我想念的地方。

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?

推荐答案

您应该反序列化键入Result而不是TResponse之类的内容

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

这篇关于XML反序列化为类错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆