Reputation: 667
I need to build the following Receipts XML structure including the xmlns:u namespace and add it to the SOAP header. So the end outgoing SOAP header should look something like this:
<Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope"
xmlns:a="http://schemas.xmlsoap.org/ws/2004/08/addressing">
<s:Header>
<u:Receipts xmlns:u="http://MyCompany/abc">
<Receipt>
<Id />
<Text />
</Receipt>
<Receipt>
<Id />
<Text />
</Receipt>
<Receipt>
<Id />
<Text />
</Receipt>
</u:Receipts>
</s:Header>
<s:Body />
</Envelope>
I'm overriding the MessageHeader class and build the xml in OnWriteHeaderContents method. However, I can’t get the correct xml/namespace. Code sample is appreciated!
Upvotes: 1
Views: 4547
Reputation: 667
This is our solution at the moment, which works fine.
public class ReceiptsHeader : MessageHeader
{
private const string HeaderName = "Receipts";
private const string HeaderNamespace = "http://MyCompany/abc";
public override string Name => HeaderName;
public override string Namespace => HeaderNamespace;
private readonly XmlDocument _usageReceipt = new XmlDocument();
public ReceiptsHeader(IEnumerable elements)
{
var headerString = new StringBuilder("<Receipts xmlns=\"http://MyCompany/abc\">");
foreach (var elem in elements)
{
// Build <Receipt> nodes here...
}
headerString.Append("</Receipts>");
_usageReceipt.LoadXml(headerString.ToString());
}
protected override void OnWriteHeaderContents(XmlDictionaryWriter writer, MessageVersion messageVersion)
{
foreach (XmlNode node in _usageReceipt.ChildNodes[0].ChildNodes)
writer.WriteNode(node.CreateNavigator(), false);
}
}
We're then adding this to the SOAP header in the BeforeSendReply method, something like this:
ReceiptsHeader head = new ReceiptsHeader(elements);
reply.Headers.Add(head);
Upvotes: 5