Scott Hanselman

[XmlRootForcePrefix(Prefix='foo')

July 29, 2003 Comment on this post [0] Posted in Web Services | XML
Sponsored By

After discovering that .NET/ASMX didn't want to let me change the prefix on the root element of a SOAP payload, I did the unthinkable, but appropriate action and wrote a SoapExtension to please the client of this Web Service.  Certainly the world of Web Services is a worse place that I had to write this.

Of course, the prefix is not needed, but it made them happy, and that is satisfaction enough, eh?  And at least it is implemented as an Attribute, so it can be easily removed in the future.

I won't ruin my karma by posting the project here, but the gist is this.  (If you want the code, and to go to hell, email me):

[WebMethod]
[SoapDocumentMethod(Action="http://www.ford.com/Sample",
RequestNamespace="http://www.ford.com/Request",
RequestElementName="GetCarRequest",
ResponseNamespace="http://www.ford.com/Response",
ResponseElementName="MyCar")]
[XmlRootForcePrefix(Prefix="scott")]
public Car HelloFord()
{
   Car c =
new Car();
  
c.Namespaces.Add("yoyoyo","hanselman");
   return c;
}

results in this (added stuff in red).  Note that the default namespace remains, and the added prefix uses that full qualified namespace.  The goal wasn't to fundamentally change the structure of the document, just add the prefix.

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
    <scott:MyCar xmlns="
http://www.ford.com/Response" xmlns:scott="http://www.ford.com/Response">
      <car xmlns:yoyoyo="hanselman" xmlns="hanselman">
        <yoyoyo:engine>some engine</yoyoyo:engine>
        <yoyoyo:color>red</yoyoyo:color>
      </car>
    </scott:MyCar >
  </soap:Body>
</soap:Envelope>

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Hosting By
Hosted in an Azure App Service

Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.