Link to home
Start Free TrialLog in
Avatar of gracesoft
gracesoft

asked on

To fetch data from XML with multiple root nodes

Hello Expert,
 
                      This is basically for hotel management software application. We would want to fetch data from XML and display that in the application.

The problem we have is that, the XML we use has two root nodes and we are not able to access the data. If it has only one root node, we are easily able to access with this code : var sections = doc.Root.Elements("RoomType").   The Root takes the default root and we are able to fetch the data but with two root nodes, we dont have an answer.

Fetch-from-XML-with-multiple-root-no.txt
This is the XML we use:

      <ProductList> and <ProductAvailRateRetrievalRS> are the two root nodes.
<ProductAvailRateRetrievalRS xmlns="http://www.expediaconnect.com/EQC/PAR/2013/07">
  	<ProductList>
    		<Hotel id="11205521" name="XDSXASXSX; Suites" city="TEXAS" />
    		<RoomType id="201099560" code="1queenBed" name="Standard Room, 1 Queen Bed" status="Active" smokingPref="NonSmoking" maxOccupants="2">
      			<BedType id="1.15" name="1 queen bed" />
      			<RatePlan id="205211028A" code="RoomOnly" name="Standard" status="Active" type="Standalone" distributionModel="HotelCollect" rateAcquisitionType="SellRate"> 
        			<Compensation default="true" percent="15.00" />
      			</RatePlan>
      			<RatePlan id="206115114A" code="RoomOnly" name="Weekly" status="Active" type="Standalone" distributionModel="HotelCollect" rateAcquisitionType="SellRate"> 
        			<Compensation default="true" percent="15.00" />
      			</RatePlan>
    		</RoomType>
    		
		<RoomType id="201099593" code="2doubleBed" name="Standard Room, 2 Double Beds" status="Active" smokingPref="NonSmoking" maxOccupants="4">
      			<BedType id="1.21" name="2 double beds" />
      			<RatePlan id="205211074A" code="RoomOnly" name="Standard" status="Active" type="Standalone" distributionModel="HotelCollect" rateAcquisitionType="SellRate"> 
        			<Compensation default="true" percent="15.00" />
      			</RatePlan>
      			<RatePlan id="206115116A" code="RoomOnly" name="Weekly" status="Active" type="Standalone" distributionModel="HotelCollect" rateAcquisitionType="SellRate"> 
        			<Compensation default="true" percent="15.00" />
      			</RatePlan>
    		</RoomType>
  	</ProductList>
</ProductAvailRateRetrievalRS>

Open in new window


And code behind file code is shown below:
public void LoadXmlDataExpedia()
    {
        string otarmtype = string.Empty;
        string roomtypelist = string.Empty;
        //string hotelidlist = string.Empty;
        int flag = 0, roomavail = 0;
        string ResponseExpedia = string.Empty;
        DropOTARoomType.Items.Add("--Select OTA RoomType--");
        string xml = "<ProductAvailRateRetrievalRQ xmlns='http://www.expediaconnect.com/EQC/PAR'> <Authentication username='xxxxxx' password='xxxxx'/>"
                       + "<Hotel id='11541'/> <ParamSet>"
                       + "<ProductRetrieval returnRateLink='true' returnRoomAttributes='true' returnRatePlanAttributes='true'/>"
                       + "</ParamSet></ProductAvailRateRetrievalRQ>";
        string url = "https://ws.expediaquickconnect.com/connect/parr";

        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);

        byte[] requestBytes = System.Text.Encoding.ASCII.GetBytes(xml);
        req.Method = "POST";
        req.ContentType = "text/xml;charset=utf-8";
        req.ContentLength = requestBytes.Length;
        Stream requestStream = req.GetRequestStream();
        requestStream.Write(requestBytes, 0, requestBytes.Length);
        requestStream.Close();
        

        HttpWebResponse res = (HttpWebResponse)req.GetResponse();
        StreamReader sr = new StreamReader(res.GetResponseStream(), System.Text.Encoding.Default);
        ResponseExpedia = sr.ReadToEnd();


        dt.Columns.Add("id", typeof(string));
        dt.Columns.Add("hotel_id", typeof(string));
        dt.Columns.Add("room_name", typeof(string));
        dt.Columns.Add("rate_name", typeof(string));
        dt.Columns.Add("Rate_Id", typeof(string));
        XmlDocument xmldoc = new XmlDocument();
        xmldoc.LoadXml(ResponseExpedia);
        
        String RateName = "", RateId = "";
        XmlElement root = xmldoc.DocumentElement;
        var doc = XDocument.Parse(ResponseExpedia);
        var sections = doc.Root.Elements("RoomType");

        foreach (var section in sections)	//Step1
        {

            XmlDocument SplitDoc = new XmlDocument();
            SplitDoc.LoadXml(Convert.ToString(section));

            DataRow dtrow = dt.NewRow();
            dtrow["id"] = Convert.ToString(SplitDoc["RoomType"].Attributes["id"].Value);
            dtrow["room_name"] = Convert.ToString(SplitDoc["RoomType"].Attributes["name"].Value);
            var GuestsectionService = section.Descendants("RatePlan");

	    //My remaining Code Here
        }
        string[] otaroomtypearry = otarmtype.Split(',');	//Step2
        string[] r1array = roomtypelist.Split(',');
        for (int i = 0; i < r1array.Length - 1; i++)
        {
            for (int j = 0; j < otaroomtypearry.Length - 1; j++)
            {
                if (otaroomtypearry[j] == r1array[[]i])
                {
                    roomavail++;
                }
            }
            if (roomavail == 0)
                DropOTARoomType.Items.Add(r1array[[]i]);

            roomavail = 0;


        }
    }

Open in new window


Step1 code becomes false and it directly jumps to Step2 code without executing the  Step1 foreach statement...
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gracesoft
gracesoft

ASKER

Thanks Mr. Fernando. The solution worked perfectly for us.

Thanks Again,
Gracesoft.
Not a problem gracesoft, glad to help. Please mark the solution as the answer to close the question.
Hi Gracesoft, if the solution posted answered the question please close the question.

Thanks and have a great day.
Are you still having issues?
Thanks for the solution.