无法将XML反序列化为C#对象

时间:2016-06-06 13:40:25

标签: c# xml deserialization

我有一个XML字符串resposne,如下所示:

<?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>

我正在上课,我想把这个反应反序列化为这样:

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

不幸的是,当我反序列化时,我得到了响应属性的子元素的空值,当我调试时,visual studio看起来像这样:

enter image description here

你可以看看我是否对变量答案进行了监视,然后我只返回一个null响应类。帮助

1 个答案:

答案 0 :(得分:0)

从GTASearchResponse上的Xml属性看起来你期待一个架构

<response><response> ... 

因为有根和属性响应。我想如果你直接将你的内容反序列化为“响应”类型它可能会起作用