3

The webresponse i got from a server is shown like this. I am able to deserialze few values. But i dont kow how to convert the Availabilty to an Array.

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

The Class for serializing is like this now.

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

Do i need to find any regular expression and put the availability in . Or is there any straight forward method. This is just a beginning am i expect issues like this much more. The web responses are not standardized thoroughly and often get some wild responses.

Anand
  • 291
  • 3
  • 13

1 Answers1

5

You need to add an array of Availability classes - something like this:

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

And then it works:

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 . . .
Pawel
  • 31,342
  • 4
  • 73
  • 104