我从服务器得到的 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/