简体   繁体   English

XML反序列化为类错误

[英]Xml Deserialization to Class Error

I'm trying to deserialize this xml string response to a class: 我试图反序列化此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>

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. 而且抛出的异常错误是: {"<result xmlns=''> was not expected."}当涉及到xml时,我总是有很多反序列化的麻烦...我知道这里有很多我想念的东西。 Help? 救命?

Hi you should deserialize to type Result instead of TResponse something like below 嗨,您应该反序列化键入Result而不是TResponse,如下所示

        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 您的存储变量现在将填充对象

Try this 尝试这个

XmlSerializer serializer = new XmlSerializer(typeof(DOTWSearchResponse.Result));
                StringReader reader = new StringReader(response);

                DOTWSearchResponse.Result deserialized = (DOTWSearchResponse.Result)serializer.Deserialize(reader);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM