Change XML Tag Prefix SOAP

1.1k Views Asked by At

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

2

There are 2 best solutions below

0
On
1
On

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/")]