Shehryar Khan
Shehryar Khan

Reputation: 61

Change XML Tag Prefix SOAP

I am trying to create a SOAP message with the prefix. 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. What I'm getting is

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <Transaction xmlns="http://tempuri.org/">
      <bankingTransaction>
        <operation parameterOrder="string">
          <fault />
          <fault />
        </operation>
        <transactionDate>dateTime</transactionDate>
        <amount>int</amount>
      </bankingTransaction>
    </Transaction>
  </soap:Body>
</soap:Envelope>

& what I actually need is

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <res:Transaction xmlns="res:http://tempuri.org/">
      <res:bankingTransaction>
        <res:operation parameterOrder="string">
          <res:fault />
          <res:fault />
        </res:operation>
        <res:transactionDate>dateTime</res:transactionDate>
        <res:amount>int</res:amount>
      </res:bankingTransaction>
    </res:Transaction>
  </soap:Body>
</soap:Envelope>

& My MassageContact is

[MessageContract]
public class BankingTransaction
{
   [MessageHeader] public Operation operation;
   [MessageHeader] public DateTime transactionDate;
   [MessageBodyMember] private unit sourceAccount;
   [MessageBodyMember] public int amount;
}

Please Help me to add prefix with my XML Elements. Thanks

Upvotes: 0

Views: 1481

Answers (2)

Abraham Qian
Abraham Qian

Reputation: 7522

We could create a MessageFormatter to customize the message format, you could refer to the following official tutorial.
https://learn.microsoft.com/en-us/dotnet/framework/wcf/extending/custom-message-formatters
Here is an example about how to do this.
https://stackoverflow.com/questions/31595770/c-sharp-wcf-global-namespaces-royal-mail/31597758#31597758
http://vanacosmin.ro/Articles/Read/WCFEnvelopeNamespacePrefix

Upvotes: 0

Popo
Popo

Reputation: 2460

You probably need to do something like this:

[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://tempuri.org/")]
[MessageContract]
public class BankingTransaction
{
   [MessageHeader] public Operation operation;
   [MessageHeader] public DateTime transactionDate;
   [MessageBodyMember] private unit sourceAccount;
   [MessageBodyMember] public int amount;
}

I am not sure how you are serializing your objects, but something like this will add the prefix:

   XmlSerializerNamespaces x = new XmlSerializerNamespaces();
    x.Add("res", "http://tempuri.org/");

add the XmlSerializerNamespaces to you serialization process maybe? It's hard to say without seeing what else you are doing. All your contracts/classes in that namespace probably need this attribute: [System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://tempuri.org/")]

Upvotes: 0

Related Questions