title: I can't properly select an XML node in a SOAP header in C#.Net
body: <p>I have an XML document wrapped in a SOAP header but I can't select a single node. I think I am just misunderstanding how to repsresent the tree when nodes are prepended with text.  e.g. wsu:Timestamp, wsu:Created</p>
  
  <p>Here is the top part of my XML:</p>
  
  <pre><code>&lt;soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsa="http://www.w3.org/2005/08/addressing" xmlns:itk="urn:nhs-itk:ns:201005"&gt;
      &lt;soap:Header&gt;
          &lt;wsa:MessageID&gt;39c6f52b-1be0-42a9-a219-5d6ececd1695&lt;/wsa:MessageID&gt;
          &lt;wsa:Action&gt;urn:nhs-itk:services:201005:SendCDADocument-v2-0&lt;/wsa:Action&gt;
          &lt;wsa:To&gt;http://127.0.0.1:4000/syncsoap&lt;/wsa:To&gt;
          &lt;wsa:From&gt;
              &lt;wsa:Address&gt;http://localhost&lt;/wsa:Address&gt;
          &lt;/wsa:From&gt;
          &lt;wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"&gt;
              &lt;wsu:Timestamp xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="D6CD5232-14CF-11DF-9423-1F9A910D4703"&gt;
                  &lt;wsu:Created&gt;2015-01-30T19:40:00&lt;/wsu:Created&gt;
                  &lt;wsu:Expires&gt;2015-01-30T19:50:00&lt;/wsu:Expires&gt;
              &lt;/wsu:Timestamp&gt;
              &lt;wsse:UsernameToken&gt;
                  &lt;wsse:Username&gt;TKS Server test&lt;/wsse:Username&gt;
              &lt;/wsse:UsernameToken&gt;
          &lt;/wsse:Security&gt;
      &lt;/soap:Header&gt;
  </code></pre>
  
  <p>Here is my code, which throws a null pointer exception on the Select Single Node because it doesn't return a match.</p>
  
  <pre><code>private static XmlDocument CreateSoapEnvelope()
  {
      XmlDocument soapEnvelop = new XmlDocument();
      DateTime myCreatedDate = DateTime.UtcNow;
  
  
      soapEnvelop.Load(@“c:\myFile.xml");
      soapEnvelop.SelectSingleNode("/soap/Envelope/Header/wsse/Security/wsu/Timestamp/Created").InnerText = myCreatedDate.ToString("yyyy-MM-ddTHH:mm:ssZ");
      return soapEnvelop;
  }
  </code></pre>
  
  <p>I triple checked for case sensitivity and I don't see a mistake there.  And when I put a breakpoint in I can see the XML is loaded into the soapEnvelop object fine.</p>
  
  <p>I implemented the suggestion of using a Namespace Manager and I could select several nodes fine. However, I have an issue with matching the Id value in wsu:Timestamp.  I tried the following but all couldn't match:</p>
  
  <pre><code>soapEnvelop.SelectSingleNode("//soap:Envelope/soap:Header/wsse:Security/wsu:Timestamp/Id", ns).InnerText = Guid.NewGuid().ToString();
  soapEnvelop.SelectSingleNode("//soap:Envelope/soap:Header/wsse:Security/wsu:Timestamp/wsu:Id", ns).InnerText = Guid.NewGuid().ToString();
  soapEnvelop.SelectSingleNode("//soap:Envelope/soap:Header/wsse:Security/wsu:Timestamp", ns).Attributes["Id"].Value = Guid.NewGuid().ToString();
  </code></pre>
  
  <p>I figured out my last problem.  If you have an attribute that is empty then the Value() call must "fail". I had modified my xml doc I was working with when I added in the code suggested below and took out the existing ID and replaced it with "", which is what I was doing with Created and Expires.  The issue must be you can have an InnerText for a node that is an empty string but you can't have that for an attribute. Maybe that is considered invalid XML so .Net says "no go dude".</p>
  
  <p>What is odd is that when I changed my code to do this call before my initial setting of Id.</p>
  
  <pre><code>XmlNode myNode = soapEnvelop.SelectSingleNode("//soap:Envelope/soap:Header/wsse:Security/wsu:Timestamp", ns);
  myNode.Attributes["wsu:Id"].Value = Guid.NewGuid().ToString();
  </code></pre>
  
  <p>It set the value fine even if Id was "" in my xml file.  That is how I stumbled onto my solution because I left my original call still in the code and it didn't throw an exception so it got me thinking the only difference is Id has a value.</p>
  
  <p>Thanks,
  Dan</p>