我有一个xml,我必须将其反序列化为对象.因为它是嵌套的后代xml,我是linq to xml的新手.我正在分享一些代码,以便您更好地理解我想要的xml.
这是我的XML
<ba:BA_Test xmlns:ba="http://www.testing.com" TimeStamp="2015-04-16T18:15:41.974+0100">
<ba:Hotels>
<ba:Hotel HotelCode="3258_811" HotelName="BEST WESTERN">
<ba:RoomTypes>
<ba:RoomType RoomTypeCode="22_3258_811" RoomTypeName="Double plus 1 adult OR 2 childr"/>
<ba:RoomType RoomTypeCode="17_3258_811" RoomTypeName="Double plus 1 adult or child"/>
<ba:RoomType RoomTypeCode="1_3258_811" RoomTypeName="Room for 1"/>
<ba:RoomType RoomTypeCode="18_3258_811" RoomTypeName="Room for 1 Adult and 1 Child"/>
<ba:RoomType RoomTypeCode="2_3258_811" RoomTypeName="Room for 2"/>
</ba:RoomTypes>
<ba:AvailabilityGroup AvailabilityGroupCode="14254_3258_811" AvailabilityGroupName="Standard">
<ba:Roomrates>
<ba:Roomrate RoomrateCode="49069_3258_811" RoomTypeCode="1_3258_811" RoomrateName="Standard Room"/>
<ba:Roomrate RoomrateCode="49071_3258_811" RoomTypeCode="2_3258_811" RoomrateName="Standard Room"/>
<ba:Roomrate RoomrateCode="49462_3258_811" RoomTypeCode="2_3258_811" RoomrateName="Vienna Highlights - Standard Room - For 2 People"/>
<ba:Roomrate RoomrateCode="58367_3258_811" RoomTypeCode="1_3258_811" RoomrateName="Standard Room - Tigrafriends"/>
<ba:Roomrate RoomrateCode="58369_3258_811" RoomTypeCode="2_3258_811" RoomrateName="Standard Room - Tigrafriends"/>
<ba:Roomrate RoomrateCode="70518_3258_811" RoomTypeCode="2_3258_811" RoomrateName="Advent days (shopping) in Vienna - Standard Room"/>
</ba:Roomrates>
</ba:AvailabilityGroup>
</ba:Hotel>
</ba:Hotels>
</ba:BA_Test>
这是我的linq到xml代码,但它不是嵌套的.
XDocument xdoc = XDocument.Load(@"D:\response.xml");
XNamespace nsr = "http://www.test.com";
// using Linq To Xml parse and load Objects
List<Hotel> hotel = (from res in xdoc.Root.Descendants(nsr + "Hotel")
select new Hotel
{
hotel_code = res.Attribute("HotelCode").Value,
hotel_name = res.Attribute("HotelName").Value,
}).ToList<Hotel>();
这里是酒店课
///
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.20329.23234")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.testing.com")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://www.testing.com", IsNullable = false)]
public partial class Hotel
{
private room_types room_typesField;
private List<availability_group> availability_groupField;
private string hotel_codeField;
private string hotel_nameField;
///
public room_types room_types
{
get
{
return this.room_typesField;
}
set
{
this.room_typesField = value;
}
}
///
public List<availability_group> availability_group
{
get
{
return this.availability_groupField;
}
set
{
this.availability_groupField = value;
}
}
在这里我如何将此xml转换为单个linq查询.任何帮助将不胜感激.谢谢.
解决方法:
这应该会给你正确的结果: –
XNamespace ns = "http://www.testing.com";
List<Hotel> result = xdoc.Descendants(ns + "Hotel")
.Select(x => new Hotel
{
HotelCode = (string)x.Attribute("HotelCode"),
HotelName = (string)x.Attribute("HotelName"),
RoomTypes = x.Descendants(ns + "RoomType")
.Select(r => new RoomType
{
RoomTypeCode = (string)r.Attribute("RoomTypeCode"),
RoomTypeName = (string)r.Attribute("RoomTypeName")
}).ToList(),
AvailabilityGroup = x.Descendants(ns + "AvailabilityGroup")
.Select(a => new availability_group
{
AvailabilityGroupCode = (string)a.Attribute("AvailabilityGroupCode"),
AvailabilityGroupName = (string)a.Attribute("AvailabilityGroupName")
}).ToList()
}).ToList();
我假设您的酒店类看起来像这样: –
public class Hotel
{
public string HotelCode { get; set; }
public string HotelName { get; set; }
public List<RoomType> RoomTypes { get; set; }
public List<availability_group> AvailabilityGroup { get; set; }
}
public class RoomType
{
public string RoomTypeCode { get; set; }
public string RoomTypeName { get; set; }
}
public class availability_group
{
public string AvailabilityGroupCode { get; set; }
public string AvailabilityGroupName { get; set; }
}
您也可以像我获取RoomType数据一样获取Roomrates数据.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。