Sample API script in C#: Get device info (read XML response)
This is an example code to demonstrate in general how to call REST API in C# and read the XML response.
Here in particular, using the REST API call for retrieving the reservations of specific user: Get Device Information
Adapted from https://github.com/PerfectoCode/Reporting-Samples/tree/master/CSharp/export-api-sample
Reference relevant libraries
Copy
using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
using System.IO;
using System.Xml;
Provide cloud and authentication details
Using same as in Sample API script in C#: Get reservations list (read JSON response)
Get XML response from REST API call retrieving info of specific device
Note:
It is possible to alter the method for other REST API operations as well by changing the operation string and the parameters needed for that operation.
(Respectively, change the method name and the parameters it would use.)
The response of the REST API operation should be in XML format.
Copy
private static XmlDocument RetrieveDeviceInfo(String deviceId)
{
String operation = "/services/handsets/" + deviceId + "?operation=info";
Dictionary<String, String> parameters = new Dictionary<String, String>();
parameters.Add("securityToken", SecurityToken);
String response = GetResponse(operation, parameters);
XmlDocument deviceInfo = new XmlDocument();
deviceInfo.LoadXml(response);
return deviceInfo;
}
Implementation of GetResponse() method
Using same as in Sample API script in C#: Get reservations list (read JSON response)
Sample usage of RetrieveDeviceInfo() method showing how to 'Read' XML response string
Note:
Here, based on the XML response format expected as per Get Device Information
Replace <device_id> with the device id you need to get information of.
Copy
public static void Main(string[] args)
{
String device = "<device_id>";
XmlDocument deviceInfo = RetrieveDeviceInfo(device);
String available = deviceInfo.GetElementsByTagName("available").Item(0).InnerXml;
if (available.Equals("true"))
{
Console.WriteLine("Device " + device + " is currently available");
}
}
Related articles
- Download an STR via REST API
- Launch Settings application on iOS devices
- Error: VTable setup of type OpenQA.Selenium.Appium.Service.AppiumCommandExecutor failed
- Legacy | Device position
- Failing to delete repository item as non-owner