我对我的一个SOAP请求有以下响应。
<?xml version="1.0" encoding="utf-8" ?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<UploadIsoResponse xmlns="http://localhost:8000/gw/">
<UploadIsoResult>false</UploadIsoResult>
<status>ISO File does not exist</status>
<md5>string</md5>
<days>9/18/2015 12:00:00 AM</days>
</UploadIsoResponse>
</soap:Body>
</soap:Envelope>
我正在使用以下代码来解析它。但是我收到错误对象引用没有设置为对象的实例。这实际上是因为从xpath返回的结果为null的值。请帮我解析单个节点。
public void fill_response_data(string xml_buffer)
{
string TARGET_NAME_SPACE = "http://localhost:8000/gw/";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xml_buffer);
XmlNamespaceManager ns = new XmlNamespaceManager(xmlDoc.NameTable);
ns.AddNamespace("msbld", TARGET_NAME_SPACE);
XmlNode md5_node = xmlDoc.SelectSingleNode("//msbld:UploadIsoResponse/md5", ns);
XmlNode md5_status_node = xmlDoc.SelectSingleNode("//msbld:UploadIsoResponse/status", ns);
txt_md5_checksum.Text = md5_node.InnerText;
txt_status.Text = md5_status_node.InnerText;
}
Exception :
System.Xml.XmlDocument在IsoGateway.exe中发生未处理的“System.NullReferenceException”类型异常
答案 0 :(得分:2)
通过在xpath中添加名称空间来解决此问题,以下是更新的代码段。
XmlNode md5_node = xmlDoc.SelectSingleNode("//msbld:UploadIsoResponse/msbld:md5", ns);
XmlNode md5_status_node = xmlDoc.SelectSingleNode("//msbld:UploadIsoResponse/msbld:status", ns);