【发布时间】:2019-03-31 11:55:42
【问题描述】:
我的肥皂反应在下面很安静。
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:doCommandResponse xmlns:ns2="http://soap.inf.hexing.cn"> <return><?xml version="1.0" encoding="UTF-8"?>
<ResponseMessage xmlns="http://iec.ch/TC57/2011/schema/message" xmlns:m="http://iec.ch/TC57/2011/MeterReadings#" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://iec.ch/TC57/2011/schema/message Message.xsd">
<Header>
<Verb>created</Verb>
<Noun>MeterReadings</Noun>
<Timestamp>2019-03-31T15:54:12+04:30</Timestamp>
<Source>HES_BASE</Source>
<AsyncReplyFlag>true</AsyncReplyFlag>
<ReplyAddress>http://192.168.15.2:8090/HES/services/DoCommandRequest?wsdl</ReplyAddress>
<AckRequired>true</AckRequired>
<User>
<UserID>abc</UserID>
</User>
<Property>
<Name>password</Name>
<Value>2222</Value>
</Property>
<MessageID>2AB290AE-5DBC-414D-8C5D-70836514E736</MessageID>
<CorrelationID>String</CorrelationID>
</Header>
<Reply>
<Result>OK</Result>
<Error>
<code>0.0</code>
</Error>
</Reply>
<Payload>
<m:MeterReadings>
<m:MeterReading>
<m:valuesInterval>
<m:end>2019-03-31T00:00:00+04:30</m:end>
<m:start>2019-03-01T00:00:00+04:30</m:start>
</m:valuesInterval>
<m:Meter>
<m:Names>
<m:name>37030298060</m:name>
<m:NameType>
<m:name>28373341367200U</m:name>
</m:NameType>
</m:Names>
<m:mRID>002997004330</m:mRID>
</m:Meter>
<m:Readings>
<m:ReadingType ref="13.8.0.6.1.1.12.0.0.0.0.0.0.0.224.3.38.0"/>
<m:ReadingQualities>
<m:ReadingQualityType ref="2.5.259"/>
</m:ReadingQualities>
<m:timePeriod>
<m:end>2019-03-31T00:00:00+04:30</m:end>
<m:start>2019-03-01T00:00:00+04:30</m:start>
</m:timePeriod>
<m:value>55.588</m:value>
<m:timeStamp>2019-03-21T00:00:00+04:30</m:timeStamp>
</m:Readings>
</m:MeterReading>
<m:Reading/>
</m:MeterReadings>
</Payload>
</ResponseMessage>
</return></ns2:doCommandResponse></soap:Body></soap:Envelope>
从上面的回复中,我想从上面的回复中获取一些数据。下面是我的代码
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 中。
为了从响应中获取值,我尝试执行以下操作
XDocument doc = XDocument.Parse(soapResult);
XmlReader xr = doc.CreateReader();
xr.ReadToFollowing("UserID");
string uid = xr.ReadElementContentAsString();
但我得到了错误
{“节点类型None不支持ReadElementContentAsString方法。第0行,位置0。”}
这个我也试过了
var result = from p in doc.Descendants()
where p.Name.LocalName == "UserID" select p.Value;
但它没有给我任何结果。
任何帮助将不胜感激
【问题讨论】:
-
SOAP 响应似乎包含一个字符串,如果将其解码为来自 SOAP 响应的值,则它本身可以被解析为 XML。 (当基于尖括号的标记作为字符串值添加到 HTML 或 XML 时,将对其内容进行编码以将其内容保留为字符串值。)
-
即。获取
<return>元素的值并将其传递给 XML 解析器以从中提取信息。 -
@Richard 所以我只需要执行以下操作
XDocument doc = XDocument.Parse(soapResult);? -
您需要从
doc中获取<result>元素的内容并对其进行解析(您已将XML 编码为XML 中的字符串)。 -
Richard 是正确的,无论是谁编写了该服务作为soap 调用的结果返回一个xml 文档,id 创建一个代理来执行soap 调用,然后尝试解析
中的 xml这将是代理类 codeproject.com/Articles/786601/… 上的字符串属性