CheckingTrt
CheckingTrt

Reputation: 41

Convert XML to object

I have a xml that I have to deserialize it to object. As it is nested descendent xml and I am new to linq to xml. I am sharing some code for your better understanding that whant i want from xml.

Here is my 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>

Here is my linq to xml code but it is not nested.

 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>();

Here the hotel class

/// 

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

Here how can i convert this xml to a single linq query. Any help will be appreciated.Thanks in advance.

Upvotes: 4

Views: 115

Answers (1)

Rahul Singh
Rahul Singh

Reputation: 21795

This should give you the correct result:-

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();

Where I have assumed your Hotel class looks like this:-

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

You can also fetch RoomRates data like the way I have fetched RoomType data.

Upvotes: 4

Related Questions