Moeez
Moeez

Reputation: 478

Unable to get values from soap response using c#

My soap response is quiet below.

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:doCommandResponse xmlns:ns2="http://soap.inf.hexing.cn">    <return>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;ResponseMessage xmlns=&quot;http://iec.ch/TC57/2011/schema/message&quot; xmlns:m=&quot;http://iec.ch/TC57/2011/MeterReadings#&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xsi:schemaLocation=&quot;http://iec.ch/TC57/2011/schema/message Message.xsd&quot;&gt;
 &lt;Header&gt;
   &lt;Verb&gt;created&lt;/Verb&gt;
    &lt;Noun&gt;MeterReadings&lt;/Noun&gt;
   &lt;Timestamp&gt;2019-03-31T15:54:12+04:30&lt;/Timestamp&gt;
  &lt;Source&gt;HES_BASE&lt;/Source&gt;
 &lt;AsyncReplyFlag&gt;true&lt;/AsyncReplyFlag&gt;
&lt;ReplyAddress&gt;http://192.168.15.2:8090/HES/services/DoCommandRequest?wsdl&lt;/ReplyAddress&gt;
&lt;AckRequired&gt;true&lt;/AckRequired&gt;
  &lt;User&gt;
     &lt;UserID&gt;abc&lt;/UserID&gt;
  &lt;/User&gt;
&lt;Property&gt;
  &lt;Name&gt;password&lt;/Name&gt;
    &lt;Value&gt;2222&lt;/Value&gt;
 &lt;/Property&gt;
 &lt;MessageID&gt;2AB290AE-5DBC-414D-8C5D-70836514E736&lt;/MessageID&gt;
 &lt;CorrelationID&gt;String&lt;/CorrelationID&gt;
&lt;/Header&gt;
&lt;Reply&gt;
&lt;Result&gt;OK&lt;/Result&gt;
&lt;Error&gt;
&lt;code&gt;0.0&lt;/code&gt;
&lt;/Error&gt;
&lt;/Reply&gt;
&lt;Payload&gt;
&lt;m:MeterReadings&gt;
&lt;m:MeterReading&gt;
&lt;m:valuesInterval&gt;
 &lt;m:end&gt;2019-03-31T00:00:00+04:30&lt;/m:end&gt;
 &lt;m:start&gt;2019-03-01T00:00:00+04:30&lt;/m:start&gt;
 &lt;/m:valuesInterval&gt;
 &lt;m:Meter&gt;
  &lt;m:Names&gt;
        &lt;m:name&gt;37030298060&lt;/m:name&gt;
     &lt;m:NameType&gt;
       &lt;m:name&gt;28373341367200U&lt;/m:name&gt;
    &lt;/m:NameType&gt;
  &lt;/m:Names&gt;
  &lt;m:mRID&gt;002997004330&lt;/m:mRID&gt;
  &lt;/m:Meter&gt;
  &lt;m:Readings&gt;
  &lt;m:ReadingType ref=&quot;13.8.0.6.1.1.12.0.0.0.0.0.0.0.224.3.38.0&quot;/&gt;
  &lt;m:ReadingQualities&gt;
   &lt;m:ReadingQualityType ref=&quot;2.5.259&quot;/&gt;
 &lt;/m:ReadingQualities&gt;
&lt;m:timePeriod&gt;
   &lt;m:end&gt;2019-03-31T00:00:00+04:30&lt;/m:end&gt;
   &lt;m:start&gt;2019-03-01T00:00:00+04:30&lt;/m:start&gt;
&lt;/m:timePeriod&gt;
&lt;m:value&gt;55.588&lt;/m:value&gt;
&lt;m:timeStamp&gt;2019-03-21T00:00:00+04:30&lt;/m:timeStamp&gt;
&lt;/m:Readings&gt;
&lt;/m:MeterReading&gt;
&lt;m:Reading/&gt;
&lt;/m:MeterReadings&gt;
&lt;/Payload&gt;
&lt;/ResponseMessage&gt;
</return></ns2:doCommandResponse></soap:Body></soap:Envelope>

From above reply I want to get some data from above response. Below is my code

 if (dt != null && dt.Rows.Count > 0)
        {
            totalRec = dt.Rows.Count;
            timer.Start();
            foreach (DataRow dr in dt.Rows)
            {

                var _url = "http://111.111.111.1:8090/HES/services/DoCommandRequest";

                string uniqueID = dr["Application_No"].ToString();
                //string meterNo = dr["METER_SERIAL_NO"].ToString();

                XmlDocument soapEnvelopeXml = CreateSoapEnvelope(uniqueID);
                HttpWebRequest webRequest = CreateWebRequest(_url);
                InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest);


                // begin async call to web request.
                IAsyncResult asyncResult = webRequest.BeginGetResponse(null, null);

                // suspend this thread until call is complete. You might want to
                // do something usefull here like update your UI.
                asyncResult.AsyncWaitHandle.WaitOne();

                // get the response from the completed web request.
                string soapResult;
                using (WebResponse webResponse = webRequest.EndGetResponse(asyncResult))
                {
                    using (StreamReader rd = new StreamReader(webResponse.GetResponseStream()))
                    {
                        soapResult = rd.ReadToEnd();
                        processedRec++;


                    }

                }

            }
            timer.Stop();
        }

In above code the response is in the string soapResult.

For getting a value from the response I tried to do the following

XDocument doc = XDocument.Parse(soapResult);
XmlReader xr = doc.CreateReader();
xr.ReadToFollowing("UserID");
string uid = xr.ReadElementContentAsString();

But I got the error

{"The ReadElementContentAsString method is not supported on node type None. Line 0, position 0."}

Also I have tried this

var result = from p in doc.Descendants() 
         where p.Name.LocalName == "UserID" select p.Value;

But it gives me no result.

Any help would be highly appreciated

Upvotes: 1

Views: 1704

Answers (2)

ONLAEG
ONLAEG

Reputation: 199

The problem is your soap response content is XML. But UserID xml element is understanded is a string - in your response content, there are many html escape character).

You need to fix your service or unescape your string by replace &lt;, &gt;, &quot;.

Upvotes: 0

Jaydeep Jadav
Jaydeep Jadav

Reputation: 836

First Get the value of the <return> element into one string variable (lets say string returnValue)

Then XML decode the returnValue using by passing the value into the following function.

public static string XmlDecode(string value) {
    var xmlDoc = new XmlDocument();
    xmlDoc.LoadXml("<root>" + value + "</root>");
    return xmlDoc.InnerText;
}

The returned value from above function is valid XML that you can parse using the XDocument.Parse or LINQ to XML

Upvotes: 1

Related Questions