How to send and receive XML data from web service

I am first time to involve a public web service project. The requirement from vendor is to send XML data to our web service. Here is the SOAP format of request which the function "AddUser" having two parameters: customer: XML format and quantity: string:

<xxx:addUserRequest xmlns:ltd=”http://yyyy.com/”>

<c:customer xmlns:c=”http://yyyy.com/customer”>

<c:customerId>259</c:customerId>

<c:Name>Joe</c:Name>

</c:customer>

<xxx:quantity>100</xxx:quantity>

</xxx:addUserRequest>

I create a web service function named "addUser" like:

public Boolean addUser(XmlDocument customer, string catalog)

But look like my web service gets the incoming SOAP body like:

<soap:Body>

<addUser xmlns:ltd=”http://yyyy.com/”>

<customer>

<customer xmlns:c=””>

<customerId>259</c:customerId>

<Name>Joe</c:Name>

</customer>

</customer>

<quantity>100</quantity>

</addUser>

</soap:Body>

You see the incoming message has extra "<customer></customer>" tag comparing with the requirement because I defined it as one of the parameter. Am I right or the web service has a way to retrieve the SOAP body request and I only need to create a function "AddUser()" without any parameters. I am not sure I ask the right question or not.

Thank you for all the help.



Answer this question

How to send and receive XML data from web service

  • dotnetpgm

    Here is some code that does what I think you want:

    using System;

    using System.Web;

    using System.Web.Services;

    using System.Web.Services.Protocols;

    using System.Xml.Serialization;

    public class Customer

    {

    public string customerID;

    public string Name;

    }

    [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();

    }

    [SoapDocumentMethod(

    RequestElementName="addUserRequest",

    RequestNamespace = "http://yyyy.com/")]

    [WebMethod]

    public string AddUser(

    [XmlElement(Namespace="http://yyyy.com/customer")] Customer customer,

    string quantity) {

    return "Thanks!";

    }

    }

    Using this code, I generated the following request message:

    < xml version="1.0" encoding="utf-8" >

    <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>

    <addUserRequest xmlns="http://yyyy.com/">

    <customer xmlns="http://yyyy.com/customer">

    <customerID>123</customerID>

    <Name>Me</Name>

    </customer>

    <quantity>100</quantity>

    </addUserRequest>

    </soap:Body>

    </soap:Envelope>

    I hope this helps!

    Daniel Roth



  • Sairam Srinivasan

    Super! Thank you very much, Daniel.

    May I ask you few more questions

    1) Do you know Java client will be able to pass in "customer" parameter in the type we defined (Class object with XMLElement type)

    2) I think the original "request message" is designed by our client in Java world with "rpc" type of document. We can do it by using "SOAPservice" instead of "ASMX". What is your view about the two choices

    3) Any other suggestions to develop web service

    Thank you for all the help.


  • How to send and receive XML data from web service