Reputation: 163
I got the following xml file from web service response, how do i retrieve the value of like say location and store into a string in visual studio? I tried the following code:
XmlNode root = wsResponseXmlDoc.DocumentElement;
XmlNode currentWeather = root.SelectSingleNode("/CurrentWeather/Location");
But it's giving me 'System.NullReferenceException' Error. Please Help!
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><GetWeatherResponse xmlns="http://www.webserviceX.NET"><GetWeatherResult><?xml version="1.0" encoding="utf-16"?>
<CurrentWeather>
<Location>Siemreap, Cambodia (VDSR) 13-22N 103-51E 15M</Location>
<Time>Dec 22, 2013 - 01:00 PM EST / 2013.12.22 1800 UTC</Time>
<Wind> Variable at 2 MPH (2 KT):0</Wind>
<Visibility> 4 mile(s):0</Visibility>
<SkyConditions> partly cloudy</SkyConditions>
<Temperature> 66 F (19 C)</Temperature>
<DewPoint> 60 F (16 C)</DewPoint>
<RelativeHumidity> 82%</RelativeHumidity>
<Pressure> 29.91 in. Hg (1013 hPa)</Pressure>
<Status>Success</Status>
</CurrentWeather></GetWeatherResult></GetWeatherResponse></soap:Body></soap:Envelope>
Upvotes: 0
Views: 108
Reputation: 4078
Two things:
If your file is really having escaped <
and >
instead of <
and >
, it won't be parsed as proper XML on those parts, rather than one big tag with tons of weird text in it.
Starting your xpath with a single /
means you're looking for root element, which obviously in your case is not found under the name CurrentWeather
. If you use //
, it means you're skipping elements until the following elements are found. Thus, try //CurrentWeather/Location
.
For more explanations about xpath syntax, see here.
Upvotes: 5