我想将值转换为带有字符串的对象。我的意思是让我说我的字符串里面有XML代码:
string response = @"<?xml version=""1.0"" encoding=""utf-8""?>
<Response>
<ResultCode>0</ResultCode>
<ResultMessage>success</ResultMessage>
<Body>
<Rooms>
<Room>
<RoomID>room1@nxdev.com</RoomID>
<RoomName>White Room</RoomName>
<Capacity>10</Capicity>
<Equipments>Beam Projector, VTR</Equipments>
<Reservations>
<Reservation>
<StartDT> 2014-02-18 14:00</StartDT>
<EndDT>2014-02-18 15:00</EndDT>
<UserName>Jhon</UserName>
<Subject>Project Meeting</Subject>
<Reservation>
</Reservatoins>
</Room>
</Rooms>
</Body>
</Response>"
我有一个Class,它具有Xml中提到的所有属性,如
[XmlRoot("Response")]
public class Response
{
string ResultCode {get; set; }
string ResutlMessage { get; set; }
}
public class RoomListResponse : Response
{
List<Room> rooms { get; set; }
}
public class Room
{
[XmlElement("RoomID")]
string RoomID { get; set; }
[XmlElement("RoomName")]
string RoomName { get; set; }
[XmlElement("Capacity")]
string Capacity { get; set; }
[XmlElement("Equipments")]
string Equipments { get; set; }
List<Reservation> Reservations;
}
public class Reservations
{
[XmlElement("StartDT")]
string StartDT { get; set; }
[XmlElement("EndDT")]
string EndDT { get; set; }
[XmlElement("UserName")]
string UserName { get; set; }
[XmlElement("Subject")]
string Subject { get; set; }
}
如何在没有字符串解析的情况下设置RoomListResponse对象中的所有值?我已尝试使用序列化,但由于API的限制,它在Windows 8.1应用商店项目中给了我一些问题。
有没有办法让它排序?
答案 0 :(得分:0)
序列化是唯一的方法。
在您的情况下,您可能想尝试替换
List<Reservation> Reservations;
通过
[XmlElement("Reservations")]
Reservation [] Reservations;
然后使用XmlSerializer