3

我从服务器获得的 webresponse 显示如下。我能够反序列化几个值。但我不知道如何将可用性转换为数组。

<AvailRateChartRS Version="1">
  <SoldMessage>sold</SoldMessage>
  <RoomTypes>
   <RoomType>
  <RoomTypeId>1</RoomTypeId>
  <SubPropertyId>1</SubPropertyId>
  <Name>Villa</Name>
  <MaxOccupancy>4</MaxOccupancy>
  <BookingRangeAvailable>false</BookingRangeAvailable>
  <Availability Id="1" Date="2012-10-16" Available="false" NoOfRoomsAvailable="0" />
  <Availability Id="2" Date="2012-10-17" Available="false" NoOfRoomsAvailable="0" />
  <Availability Id="3" Date="2012-10-18" Available="false" NoOfRoomsAvailable="0" />
  <Availability Id="4" Date="2012-10-19" Available="false" NoOfRoomsAvailable="0" />
  <Availability Id="5" Date="2012-10-20" Available="false" NoOfRoomsAvailable="0" />
  <Availability Id="6" Date="2012-10-21" Available="false" NoOfRoomsAvailable="0" />
</RoomType>
</RoomTypes>
</AvailRateChartRS>

序列化的类现在是这样的。

namespace Classes
 {

[Serializable()]
public class RoomType
{
    [XmlElement("RoomTypeId")]
    public string RoomTypeId { get; set; }

    [XmlElement("SubPropertyId")]
    public string SubPropertyId { get; set; }

    [XmlElement("Name")]
    public string Name { get; set; }

    [XmlElement("MaxOccupancy")]
    public string MaxOccupancy { get; set; }

    [XmlElement("BookingRangeAvailable")]
    public string BookingRangeAvailable { get; set; }

    [XmlElement("Availability")]
    public string Availability { get; set; }

}


[Serializable()]
[System.Xml.Serialization.XmlRoot("AvailRateChartRS")]
public class RoomTypes
{
    [XmlArray("RoomTypes")]
    [XmlArrayItem("RoomType", typeof(RoomType))]
    public RoomType[] RoomType { get; set; }
}
 }

我是否需要找到任何正则表达式并将可用性放入 . 或者有没有直接的方法。这只是一个开始,我期待更多这样的问题。网络响应没有彻底标准化,并且经常得到一些疯狂的响应。

4

1 回答 1

5

您需要添加一系列可用性类 - 如下所示:

        [Serializable()]
        public class RoomType
        {
            [XmlElement("RoomTypeId")]
            public string RoomTypeId { get; set; }

            [XmlElement("SubPropertyId")]
            public string SubPropertyId { get; set; }

            [XmlElement("Name")]
            public string Name { get; set; }

            [XmlElement("MaxOccupancy")]
            public string MaxOccupancy { get; set; }

            [XmlElement("BookingRangeAvailable")]
            public string BookingRangeAvailable { get; set; }

            [XmlElement("Availability")]
            public Availability[] Availability { get; set; }
        }

        public class Availability
        {
            [XmlAttribute]
            public int Id { get; set; }

            [XmlAttribute]
            public string Date { get; set; }

            [XmlAttribute]
            public bool Available { get; set; }

            [XmlAttribute]
            public int NoOfRoomsAvailable { get; set; }
        }


        [Serializable()]
        [System.Xml.Serialization.XmlRoot("AvailRateChartRS")]
        public class RoomTypes
        {
            [XmlArray("RoomTypes")]
            [XmlArrayItem("RoomType", typeof(RoomType))]
            public RoomType[] RoomType { get; set; }
        }


        class Program
        {
            const string xml =
    @"<AvailRateChartRS Version=""1""> 
        <SoldMessage>sold</SoldMessage> 
        <RoomTypes> 
          <RoomType> 
            <RoomTypeId>1</RoomTypeId> 
            <SubPropertyId>1</SubPropertyId> 
            <Name>Villa</Name> 
            <MaxOccupancy>4</MaxOccupancy> 
            <BookingRangeAvailable>false</BookingRangeAvailable> 
            <Availability Id=""1"" Date=""2012-10-16"" Available=""false"" NoOfRoomsAvailable=""0"" /> 
            <Availability Id=""2"" Date=""2012-10-17"" Available=""false"" NoOfRoomsAvailable=""0"" /> 
            <Availability Id=""3"" Date=""2012-10-18"" Available=""false"" NoOfRoomsAvailable=""0"" /> 
            <Availability Id=""4"" Date=""2012-10-19"" Available=""false"" NoOfRoomsAvailable=""0"" /> 
            <Availability Id=""5"" Date=""2012-10-20"" Available=""false"" NoOfRoomsAvailable=""0"" /> 
            <Availability Id=""6"" Date=""2012-10-21"" Available=""false"" NoOfRoomsAvailable=""0"" /> 
          </RoomType> 
        </RoomTypes> 
      </AvailRateChartRS>";

            static void Main(string[] args)
            {
                var serializer = new XmlSerializer(typeof(RoomTypes));

                var availChart = (RoomTypes)serializer.Deserialize(new StringReader(xml));

                foreach(var availability in availChart.RoomType.First().Availability)
                {
                    Console.WriteLine("Availability {0} {1} {2} {3}", availability.Id, availability.Date, availability.Available, availability.NoOfRoomsAvailable);
                }
            }
        }

然后它起作用了:

Availability 1 2012-10-16 False 0
Availability 2 2012-10-17 False 0
Availability 3 2012-10-18 False 0
Availability 4 2012-10-19 False 0
Availability 5 2012-10-20 False 0
Availability 6 2012-10-21 False 0
Press any key to continue . . .
于 2012-10-12T17:55:06.953 回答