xml - 如何使用重复的 XMLNodes 反序列化此 xml

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

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

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

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

最佳答案

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

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

关于xml - 如何使用重复的 XMLNodes 反序列化此 xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12859690/

相关文章:

c# - 处理未定义抽象方法C#的调用

python - 需要在 Python 中使用 BeautifulSoup 将 XML 文件作为流读取

java - LinearLayout嵌套到RelativeLayout时设置LinearLayout的LayoutParams

xml - 如何有效地为基于 XML 的文本协议(protocol)定义传输结束?

asp.net-mvc - 如何在不终止用户 session 的情况下部署 MVC 4 应用程序

c# - MVC 3(预览版 1)动态 View 模型

c - 反序列化错误

java - Android工作室:Configure Project in android studio

c#-4.0 - C# 4. 0's new "命名参数”功能不应该被称为 "named arguments"吗?

c# - 将 Azure 搜索结果反序列化为你自己的模型的最佳方法是什么?