locked
Change Object name in generated WSDL RRS feed

  • Question

  •  

    Good day,

     

    I have a Web Method that uses a simple custom object, name Client, as a parameter, and also returns a Client.

     However, in the generated WSDL, I want "element name="Client" " to be changed to "element name="THE_CLIENT", but without changing the class name.

     Is there a way to specify that an object should have another name in the wsdl, Something like [WebMethod(Description="DESCRIPTION")], but for a class?

    Thank you, have a nice day!

     

    Thursday, September 6, 2007 1:51 PM

Answers

  • Absolutely.  ASMX services use the XmlSerializer to serialize its data types, so you can use attributes to customize how instances are serialized.  For details see Controlling XML Serialization Using Attributes.

     

    In the request message the element name for each parameter in the webmethod is derived from the parameter name.  You can use the XmlElementAttribute on each parameter to change the name that is used.

     

    In the response message, the response element name is generally derived from the webmethod name with "Reponse" appended.  You can also use the XmlElementAttribute on the return value using the [result:XmlElement(ElementName="blah")] syntax.

     

    Here is a working sample:

     

    Code Snippet

    using System;

    using System.Web;

    using System.Web.Services;

    using System.Web.Services.Protocols;

    using System.Xml.Serialization;

    [WebService(Namespace = "http://tempuri.org/")]

    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

    public class Service : System.Web.Services.WebService

    {

    public Service () {

    //Uncomment the following line if using designed components

    //InitializeComponent();

    }

    [WebMethod]

    [return:XmlElement(ElementName="THE_CLIENT")]

    public Client HelloWorld([XmlElement(ElementName="THE_CLIENT")] Client client) {

    return client;

    }

    }

    public class Client

    {

    private string name;

    public string Name

    {

    get { return name; }

    set { this.name = value; }

    }

    }

     

     

    Daniel Roth
    Thursday, September 6, 2007 5:25 PM

All replies

  • Absolutely.  ASMX services use the XmlSerializer to serialize its data types, so you can use attributes to customize how instances are serialized.  For details see Controlling XML Serialization Using Attributes.

     

    In the request message the element name for each parameter in the webmethod is derived from the parameter name.  You can use the XmlElementAttribute on each parameter to change the name that is used.

     

    In the response message, the response element name is generally derived from the webmethod name with "Reponse" appended.  You can also use the XmlElementAttribute on the return value using the [result:XmlElement(ElementName="blah")] syntax.

     

    Here is a working sample:

     

    Code Snippet

    using System;

    using System.Web;

    using System.Web.Services;

    using System.Web.Services.Protocols;

    using System.Xml.Serialization;

    [WebService(Namespace = "http://tempuri.org/")]

    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

    public class Service : System.Web.Services.WebService

    {

    public Service () {

    //Uncomment the following line if using designed components

    //InitializeComponent();

    }

    [WebMethod]

    [return:XmlElement(ElementName="THE_CLIENT")]

    public Client HelloWorld([XmlElement(ElementName="THE_CLIENT")] Client client) {

    return client;

    }

    }

    public class Client

    {

    private string name;

    public string Name

    {

    get { return name; }

    set { this.name = value; }

    }

    }

     

     

    Daniel Roth
    Thursday, September 6, 2007 5:25 PM
  • Perfect!

     

    Thank you very much.

     

    I also found out that I can put

    [XmlType("THECLIENT")]

     

    above the declaration of the class  (   public class Client { ... } ) 

    What's the best way to proceed, your solution or this one?

     

    Thank you

     

    Thursday, September 6, 2007 5:34 PM