0

I'm using following code to add header to soap header.

using (new OperationContextScope(client.InnerChannel))
{
    OperationContext.Current.OutgoingMessageHeaders.Add(
        MessageHeader.CreateHeader("ns:To", "", "http://###.com"));                                
}

But I got undeclared namespace prefix ns error, I will need add xmlns:ns="http://www.w3.org/2005/08/addressing" to name space.

So my question is how to add namespace prefix to

<s:Envelope>
    <s:Header>
        <ns:To>##</ns:To>
    </s:Header>
</s:Envelope>

To

<s:Envelope>
    <s:Header xmlns:ns="http://www.w3.org/2005/08/addressing">
        <ns:To>##</ns:To>
    </s:Header>
</s:Envelope>
ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
DanielZ
  • 303
  • 3
  • 14
  • 1
    May be this will help you ... https://social.msdn.microsoft.com/Forums/vstudio/en-US/05a561cd-879b-4dc7-ac1a-88a879272400/how-to-add-attribute-to-wcf-message-header-with-messageheadercreateheader-method?forum=wcf – mukesh kudi Aug 04 '18 at 13:04
  • See my solution at following posting : https://stackoverflow.com/questions/46722997/saml-assertion-in-a-xml-using-c-sharp/46724392#comment80642919_46724392 – jdweng Aug 04 '18 at 13:11
  • @m.kudi thank you for the reply, put in my code, but not add namespace to header. – DanielZ Aug 04 '18 at 20:38

1 Answers1

0

Try this:

String oasisWsSecNS = "http://www.w3.org/2005/08/addressing";
XmlDocument document = new XmlDocument();
     
XmlElement child = document.CreateElement("To", oasisWsSecNS);
child.AppendChild(document.CreateTextNode("[value]"));
element.AppendChild(child);
            
context.OutgoingMessageHeaders.Add(MessageHeader.CreateHeader("Header", oasisWsSecNS, document.DocumentElement));
Nirbs
  • 1
  • 1