1

I have following response for one of my SOAP request.

<?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>

And I am using following code to parse it. However I am getting an error Object reference not set to an instance of an object. This is actually because of the resulting null value returned from the xpath. Please help me on parsing single node.

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.XmlDocumentAn unhandled exception of type 'System.NullReferenceException' occurred in IsoGateway.exe

1
  • 1
    I'd suggest using LINQ to XML rather than the XmlDocument DOM and XPath. It'd be much nicer to work with. Commented Sep 13, 2015 at 10:13

1 Answer 1

2

Fixed the issue by adding name space in xpath and below is the updated snippet.

  XmlNode md5_node = xmlDoc.SelectSingleNode("//msbld:UploadIsoResponse/msbld:md5", ns);
     XmlNode md5_status_node = xmlDoc.SelectSingleNode("//msbld:UploadIsoResponse/msbld:status", ns);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.