如何使用重复的XML节点反序列化xml

时间:2012-10-12 13:02:26

标签: xml c#-4.0 deserialization xml-deserialization

我从服务器获得的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; }
}
 }

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

1 个答案:

答案 0 :(得分:4)

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

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