无法从肥皂反应使用C#获取值 [英] Unable to get values from soap response using c#

查看:68
本文介绍了无法从肥皂反应使用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();
        }

在上面的代码中,响应位于字符串 soapResult中

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();

但是我得到了错误


{节点类型无不支持ReadElementContentAsString方法。第0行,位置0。}

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

我也试过了

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

但是我没有结果。

任何帮助将不胜感激

推荐答案

首先获取< return>的值元素转换为一个字符串变量(比如说 string returnValue

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

然后XML解码 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;
}

上述函数返回的值是有效的XML,您可以使用 XDocument.Parse 或LINQ to XML

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

这篇关于无法从肥皂反应使用C#获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆