Processing XML replies

Parent Previous Next

Often web services reply with a XML document specific to the web service.

IYOPRO supports the parsing of these XML replies. The C# XML processing can be used to easily process a XML reply.


Example:

Assume the reply from the web service is a XML document like this:


<?xml version="1.0" encoding="utf-16"?>

<CurrentWeather>

 <Location>Hamburg-Finkenwerder, Germany (EDHI) 53-32N 009-50E 13M</Location>

 <Time>Jun 27, 2016 - 08:50 AM EDT / 2016.06.27 1250 UTC</Time>

 <Wind> from the SSW (210 degrees) at 14 MPH (12 KT) (direction variable):0</Wind>

 <Visibility> greater than 7 mile(s):0</Visibility>

 <SkyConditions> mostly cloudy</SkyConditions>

 <Temperature> 69 F (21 C)</Temperature>

 <DewPoint> 51 F (11 C)</DewPoint>

 <RelativeHumidity> 52%</RelativeHumidity>

 <Pressure> 29.97 in. Hg (1015 hPa)</Pressure>

 <Status>Success</Status>

</CurrentWeather>


However you are only interested in the relative humidity. Using the C# XML processing one can obtain the value e.g. like this:


import clr

clr.AddReference('System.Xml')

from System.Xml import XmlDocument

       

xmlDoc = XmlDocument()

xmlDoc.LoadXml(Value)

       

nodes = xmlDoc.SelectNodes("/CurrentWeather/RelativeHumidity")

for node in nodes:

   Result = node.InnerText

   break


It is also possible and often easier to use the XML 2 Linq approach to optain the value:


import clr

clr.AddReference('System.Xml.Linq')

from System.Xml.Linq import XElement

xmlElement = XElement.Parse(Value)

Result = xmlElement.Element("RelativeHumidity").Value