gpt4 book ai didi

c# - 将 XML 转换为对象

转载 作者:数据小太阳 更新时间:2023-10-29 02:06:13 29 4
gpt4 key购买 nike

我有一个 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 to 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();

我假设您的 Hotel 类如下所示:-

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 数据。

关于c# - 将 XML 转换为对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29749419/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com