title: Read xml attribute in C#
body: <p>I have been searching this for a while and cannot find a solution.  I have an xml that is returned from a HttpWebRequest that I load into an xmldocument and I am trying to get a specific attribute (status) of the request.  The xml that is returned is below.</p>
  
  <pre><code>&lt;soapenv:Envelope
    xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    &gt;
    &lt;soapenv:Body&gt;
      &lt;processRequestResponse&gt;
        &lt;parameters&gt;
          &lt;ns1:searchResponse status="success" xmlns:ns1="urn:oasis:names:tc:SPML:2:0:search"&gt;
            &lt;ns1:pso&gt;
              &lt;ns2:psoID ID="Users:####" xmlns:ns2="urn:oasis:names:tc:SPML:2:0"/&gt;
              &lt;ns3:data xmlns:ns3="urn:oasis:names:tc:SPML:2:0"&gt;
                &lt;ns4:attr name="Users.User ID" xmlns:ns4="urn:oasis:names:tc:DSML:2:0:core"&gt;
                  &lt;ns4:value&gt;&lt;/ns4:value&gt;
                &lt;/ns4:attr&gt;
              &lt;/ns3:data&gt;
            &lt;/ns1:pso&gt;
          &lt;/ns1:searchResponse&gt;
        &lt;/parameters&gt;
      &lt;/processRequestResponse&gt;
    &lt;/soapenv:Body&gt;
  &lt;/soapenv:Envelope&gt;
  </code></pre>
  
  <p>My code that I am using to get this data is below.</p>
  
  <pre><code>HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  StreamReader reader = new StreamReader(response.GetResponseStream());
  string returnResponse = reader.ReadToEnd();
  
  XmlDocument doc = new XmlDocument();
  doc.LoadXml(returnResponse);
  
  XmlNode root = doc.DocumentElement;
  XmlNode searchResponse = root.SelectSingleNode("Envelope/Body/processRequestResponse/parameters/searchResponse");
  XmlAttribute status = (XmlAttribute)searchResponse.Attributes.GetNamedItem("status");
  if (status != null)
  {
      string statusReturn = status.Value;
      return statusReturn;
  }
  else
  {
      return "value is null";
  }
  </code></pre>
  
  <p>Any help you can give in the status value would be appreciated.  I keep getting an object reference error at the xmlattrbute status line.</p>