下面我的肥皂反应很安静。
<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();
但是我得到了错误
{“节点类型“无”不支持ReadElementContentAsString方法。第0行,位置0。”}
我也尝试过
var result = from p in doc.Descendants()
where p.Name.LocalName == "UserID" select p.Value;
但是我没有结果。
任何帮助将不胜感激
答案 0 :(得分:1)
首先将<return>
元素的值放入一个字符串变量中(让我们说string returnValue
)
然后,通过将值传递到以下函数中来对returnValue
进行XML解码。
public static string XmlDecode(string value) {
var xmlDoc = new XmlDocument();
xmlDoc.LoadXml("<root>" + value + "</root>");
return xmlDoc.InnerText;
}
上述函数返回的值是有效的XML,您可以使用XDocument.Parse
或LINQ to XML来解析
答案 1 :(得分:0)
问题在于您的肥皂响应内容是XML。但是UserID xml元素被理解为字符串-在您的响应内容中,有许多html转义字符)。
您需要通过替换<
,>
,"
来解决服务问题或取消转义字符串。