I am trying to create a SOAP message to send to an SAP endpoint, however, I am having trouble setting the namespace correctly. I have been trying for days and tried many suggestions I found online, but none seem to work. I am hoping some of you can help me.
This is an example of the C# code that I have:
[ServiceContract]
[XmlSerializerFormat]
public interface IWcfClient
{
[OperationContract(IsOneWay = true)]
void SendUpdates(UpdateRequest request);
}
[MessageContract(IsWrapped = true, WrapperName = "MyRoot", WrapperNamespace = "myNamespace")]
public class UpdateRequest
{
[MessageBodyMember]
[XmlElement(ElementName = "updateType")]
public byte UpdateType { get; internal set; }
[MessageBodyMember]
[XmlElement(ElementName = "updateEntry")]
public UpdateEntry[] UpdateEntries { get; set; }
}
public class UpdateEntry
{
[XmlElement]
public string DeviceId { get; set; }
[XmlElement]
public DateTime LastSeen { get; set; }
}
This results in the SOAP body looking something like this:
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<MyRoot xmlns="myNamespace">
<updateEntry>
<DeviceId>12345</DeviceId>
<LastSeen>2017-04-24T14:44:30.8030649Z</LastSeen>
</updateEntry>
<updateEntry>
<DeviceId>56789</DeviceId>
<LastSeen>2017-05-03T01:33:02.084Z</LastSeen>
</updateEntry>
<updateType>2</updateType>
</MyRoot>
</s:Body>
What I am trying to achieve, is to add another namespace to the root and have the sub-elements use that namespace. In end effect I am looking for a result similar to this (mySecondNamespace):
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<MyRoot xmlns="myNamespace" xmlns:a="mySecondNamespace">
<a:updateEntry>
<a:DeviceId>12345</a:DeviceId>
<a:LastSeen>2017-04-24T14:44:30.8030649Z</a:LastSeen>
</a:updateEntry>
<a:updateEntry>
<a:DeviceId>56789</a:DeviceId>
<a:LastSeen>2017-05-03T01:33:02.084Z</a:LastSeen>
</a:updateEntry>
<a:updateType>2</a:updateType>
</MyRoot>
</s:Body>
I have tried:
- Adding XmlRoot to the MessageContract
- Adding XmlType to the MessageContract
- Using "XmlNamespaceDeclarations" attribute on a getter/setter in which the new namespace is added to the namespace collection
- Setting the namespace on the OperationContract
But nothing seems to work. Adding the namespace to the XmlElement seems to have effect, but in that case the namespace is not on the root element and is not propagated to the underlying elements. Any ideas?