我正在尝试从使用 SoapUI 发送的以下 XML 片段中获取“有效负载”的值:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:loc="localhost">
<soapenv:Header/>
<soapenv:Body>
<loc:HTTPWebService>
<!--Optional:-->
<loc:input>Test</loc:input>
<payload>Data</payload>
</loc:HTTPWebService>
</soapenv:Body>
</soapenv:Envelope>
这是 C# 位:
string xmlPayload = "";
XmlDocument xmlSoapRequest = new XmlDocument();
using (Stream receiveStream = HttpContext.Current.Request.InputStream)
{
receiveStream.Position = 0;
using (StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8))
{
xmlSoapRequest.Load(readStream);
xmlPayload = xmlSoapRequest.SelectSingleNode("/payload/@value").Value;
}
}
return xmlPayload;
问题是当前“xmlPayload”的值为 null 并且请求失败。XML 已正确加载,但由于某种原因,“XmlPayload”为空,SoapUi 以“System.NullReferenceException:未设置对象实例的对象引用”进行响应。如何正确解析“有效负载”中的“数据”值?