将XML转换为对象

时间:2015-04-20 13:33:32

标签: c# xml linq

我有一个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查询。任何帮助将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:4)

这应该会给你正确的结果: -

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

您还可以像我提取RoomRates数据一样获取RoomType数据。