Page 1 of 1

Need help on XML parser in Ranorex

Posted: Mon Dec 08, 2014 12:58 pm
by subhashree
Hi,

I am getting a REST api XML response. I am storing the response in string format. Now I want to parse the xml and get some node values. Could you please help me in the code?
this fetches the response

String response = client.MakeRequest();

Now I want to parse the response, so I am using following code
string hoursworked = testxml.Parser(response);

parser function is as follows

public string Parser(string res)

{
XmlDocument doc = new XmlDocument();

doc.LoadXml(res);
XmlNodeList nodeList = doc.DocumentElement.SelectNodes("/AttLeaveDetails/AttendanceList/SelfAttendanceDetails");
string hrwrkd = "",reqdhrs="";
Report.Log(ReportLevel.Info, "xml request generated is"+doc);
foreach (XmlNode node in nodeList)
{
hrwrkd = node.SelectSingleNode("HoursWorked").InnerText;
reqdhrs = node.SelectSingleNode("RequiredHours").InnerText;
Report.Log(ReportLevel.Info,"hours worked is" + hrwrkd);
Report.Log(ReportLevel.Info,"required hours is" + reqdhrs);
}
return hrwrkd;

however when I run it, it seems the xml response is not properly converted from string to xml and due to this I am not able to parse it. Could you please provide help on it?
}

Re: Need help on XML parser in Ranorex

Posted: Mon Dec 08, 2014 1:32 pm
by CookieMonster
Hi Subhashree,

Did you get an error message or you are not getting any values from the node, you selected?
As far as I know, if you are using the XmlDocument you have to define the namespaces of the Xml to get the values of the node.

But you can also use XDocument object to get the values. But you have to use .Net 3.5 or higher.

Code: Select all

using System.Xml.Linq;
using System.Xml.XPath;
Here you can do something like

Code: Select all

XDocument xDocument = XDocument.Parse(res);

XPathNavigator xPathNavigator = xDocument.CreateNavigator();

XPathNodeIterator xNodeIterator = xPathNavigator.Select("/AttLeaveDetails/AttendanceList/SelfAttendanceDetails/HoursWorked");

//or you can use a while
if (xNodeIterator.MoveNext())
{
          Report.Log(ReportLevel.Info,"hours worked is" + xNodeIterator.Current.Value);
}

Cheers
Dan

Re: Need help on XML parser in Ranorex

Posted: Wed Dec 10, 2014 8:22 am
by subhashree
Hi Dan,

thanks for your response. I tried this code, I got some response however that is not the required one. My XML is something like as below

<AttLeaveDetails>
<AdvncExcesList>
<AdvanceExcessLeave>
<AppliedLeaveCount>0</AppliedLeaveCount>
<LeaveType>Advance leaves consumed</LeaveType>
<Message>Leave details fetched successfully.</Message>
<Status>true</Status>
</AdvanceExcessLeave>
<AdvanceExcessLeave>
<AppliedLeaveCount>0</AppliedLeaveCount>
<LeaveType>Excess leaves encashable</LeaveType>
<Message>Leave details fetched successfully.</Message>
<Status>true</Status>
</AdvanceExcessLeave>
</AdvncExcesList>
<AttendanceList>
<SelfAttendanceDetails>
<Dailyattendancediscrepancyurl>
https://sample?urlsample123.aspx
</Dailyattendancediscrepancyurl>
<Discrepancy>0</Discrepancy>
<HoursWorked>00:00</HoursWorked>
<Message>Attendance details fetched successfully.</Message>
<RequiredHours>00:00</RequiredHours>
<Status>true</Status>
<workinghrsinthismonthurl>
https://sample?url=/1234.aspx
</workinghrsinthismonthurl>
</SelfAttendanceDetails>
</AttendanceList>
<LeavesList></LeavesList>
<Message>Data has been successfully retrieved.</Message>
<Status>true</Status>
</AttLeaveDetails>

from where I am trying to fetch the result for "Hours worked" tag. When I executed the code provided by you, it returns me the value of each & every node inspite of only the "Hours worked" node.

Following is the response which I am receiving.

0Advance leaves consumedLeave details fetched successfully.true0Excess leaves encashableLeave details fetched successfully.truehttps://sample?urlsample123.aspx000:00Attendance details fetched successfully.00:00truehttps://sample?url=/eis/LMS/webui/LeaveReports/AttendanceTransaction.aspx0https://sample?urlabcd.aspx17.75Privileged LeaveLeave details fetched successfully.truehttps://sample?urlxyz.aspx?Type=PL0https://sample?urlabcd.aspx0Optional LeaveLeave details fetched successfully.truehttps://sample?urlxyz.aspx?Type=OL0https://sample?urlabcd.aspx0Optional HolidayLeave details fetched successfully.truehttps://sample?urlxyz.aspx?Type=OHL0https://sample?urlabcd.aspx0Compensatory OffLeave details fetched successfully.truehttps://sample?urlxyz.aspx?Type=CompOffData has been successfully retrieved.true

Could you please provide your help on this?

Re: Need help on XML parser in Ranorex

Posted: Wed Dec 10, 2014 1:48 pm
by CookieMonster
Hi Subhashree,

I prepared an simple Ranorex Solution how it could work.
Please download the zip file.
May this is a kind of a solution for you, if not please let me know or if you need more help.

Cheers
Dan

Re: Need help on XML parser in Ranorex

Posted: Thu Dec 11, 2014 12:37 pm
by subhashree
Hi Dan,
thank you so much indeed for the response. Your code ran as expected when I ran it as a different solution. However when I merged it to my code, no luck here :(. I am still getting all tag values(as before) as output rather than value for hoursworked tag value.

I figured out one difference which might be the reason. The XML which I had provided you yesterday was the one which I get from rest client & not from code. Its my mistake of providing wrong information. With the xml file which you have taken as input, your code works perfectly. However with the xml file received from code, its not working as expected.

I am getting the response as a string to which I am converting to xml and after conversion its shown as below i.e. its taking the namespace

<AttLeaveDetails xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07 ... nce.Models">
<AdvncExcesList>
<AdvanceExcessLeave>
<AppliedLeaveCount>0</AppliedLeaveCount>
<LeaveType>Advance leaves consumed</LeaveType>
<Message>Leave details fetched successfully.</Message>
<Status>true</Status>
</AdvanceExcessLeave>
<AdvanceExcessLeave>
<AppliedLeaveCount>0</AppliedLeaveCount>
<LeaveType>Excess leaves encashable</LeaveType>
<Message>Leave details fetched successfully.</Message>
<Status>true</Status>
</AdvanceExcessLeave>
</AdvncExcesList>
<AttendanceList>
<SelfAttendanceDetails>
<Dailyattendancediscrepancyurl>https://test1234.aspx</Dailyattendancediscrepancyurl>
<Discrepancy>0</Discrepancy>
<HoursWorked>00:00</HoursWorked>
<Message>Attendance details fetched successfully.</Message>
<RequiredHours>00:00</RequiredHours>
<Status>true</Status>
<workinghrsinthismonthurl>https://test123.aspx</workinghrsinthismonthurl>
</SelfAttendanceDetails>
</AttendanceList>
<LeavesList>
<SelfLeaveDetails>
<AppliedLeaveCount>0</AppliedLeaveCount>
<ApplyLeaveURL>https://test.aspx</ApplyLeaveURL>
<AvailableLeaveCount>17.75</AvailableLeaveCount>
<LeaveType>Privileged Leave</LeaveType>
<Message>Leave details fetched successfully.</Message>
<Status>true</Status>
<leaveurl>https://sample?Type=PL</leaveurl>
</SelfLeaveDetails>
<SelfLeaveDetails>
<AppliedLeaveCount>0</AppliedLeaveCount>
<ApplyLeaveURL>https://test.aspx</ApplyLeaveURL>
<AvailableLeaveCount>0</AvailableLeaveCount>
<LeaveType>Optional Leave</LeaveType>
<Message>Leave details fetched successfully.</Message>
<Status>true</Status>
<leaveurl>https://sample?Type=OL</leaveurl>
</SelfLeaveDetails>
<SelfLeaveDetails>
<AppliedLeaveCount>0</AppliedLeaveCount>
<ApplyLeaveURL>https://test.aspx</ApplyLeaveURL>
<AvailableLeaveCount>0</AvailableLeaveCount>
<LeaveType>Optional Holiday</LeaveType>
<Message>Leave details fetched successfully.</Message>
<Status>true</Status>
<leaveurl>https://sample?Type=OHL</leaveurl>
</SelfLeaveDetails>
<SelfLeaveDetails>
<AppliedLeaveCount>0</AppliedLeaveCount>
<ApplyLeaveURL>https://test.aspx</ApplyLeaveURL>
<AvailableLeaveCount>0</AvailableLeaveCount>
<LeaveType>Compensatory Off</LeaveType>
<Message>Leave details fetched successfully.</Message>
<Status>true</Status>
<leaveurl>https://sample?Type=CompOff</leaveurl>
</SelfLeaveDetails>
</LeavesList>
<Message>Data has been successfully retrieved.</Message>
<Status>true</Status>
</AttLeaveDetails>

could you please provide me your help on it?

Regards
Subhashree

Re: Need help on XML parser in Ranorex

Posted: Thu Dec 11, 2014 2:18 pm
by CookieMonster
Hi

You have to change and to add following code line:

After Class Recording1 you have to add:
private XmlNamespaceManager nameSpaceManager = null;

In the method LoadXmlFile you have to add:

this.nameSpaceManager = new XmlNamespaceManager(this.xPathNavi.NameTable);
this.nameSpaceManager.AddNamespace("hello", "http://schemas.datacontract.org/2004/07 ... nce.Models");


In the method EvaluateXml you have to add in the Select method as second parameter this.nameSpaceManager:

XPathNodeIterator xNodeIterator = xPathNavi.Select(xmlPath, this.nameSpaceManager);

And the XPath has to look like:
//hello:AttLeaveDetails//hello:HoursWorked
....

Regards
Dan

Code: Select all

public partial class Recording1
	{
		private XPathNavigator xPathNavi = null;
		private XmlNamespaceManager nameSpaceManager = null;
		/// <summary>
		/// This method gets called right after the recording has been started.
		/// It can be used to execute recording specific initialization code.
		/// </summary>
		private void Init()
		{
			// Your recording specific initialization code goes here.
		}
		
		private void LoadXmlFile(string fileName)
		{
			string pathToFile = AppDomain.CurrentDomain.BaseDirectory + "Input\\" + fileName;
			
			string fileContent = string.Empty;

			if (File.Exists(pathToFile))
			{
				using (StreamReader sr = new StreamReader(pathToFile))
				{
					fileContent = sr.ReadToEnd();
					Report.Log(ReportLevel.Success, "Success", "File successfully read");
				}
			}
			else
			{
				Report.Log(ReportLevel.Error, "Exception", "Hatschi, I'm Sick");
			}
			
			XDocument xDocument = XDocument.Parse(fileContent);
			this.xPathNavi = xDocument.CreateNavigator();
			this.nameSpaceManager = new XmlNamespaceManager(this.xPathNavi.NameTable);
			this.nameSpaceManager.AddNamespace("hello", "http://schemas.datacontract.org/2004/07/LeaveAttendance.Models");

		}
		
		private void EvaluateXml(string xmlPath)
		{
			XPathNodeIterator xNodeIterator = xPathNavi.Select(xmlPath, this.nameSpaceManager);
			
			while(xNodeIterator.MoveNext())
			{
				Report.Log(ReportLevel.Info, "Node Data", "Node Name: " + xNodeIterator.Current.Name + " Value: " + xNodeIterator.Current.Value);
			}
		}

	}

Re: Need help on XML parser in Ranorex

Posted: Fri Dec 12, 2014 5:48 am
by subhashree
It worked!!! Thanks a lot indeed Dan..

Regards
Subhashree