locked
Why the XmlElement's object is null?

    Question

  • Hi, I'm a beginner in web service developing.

    I trying to create a C# Client that sends SOAP requests to a web service through HTTP POST method.

    My web service's code is the following:

    <%@ WebService Language="C#" Class="Demo1"%>
    
    using System.Web.Services;
    using System.Xml;
    using System.IO;
    
    [WebService(Namespace="urn:Demo1")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class Demo1 : WebService{
    
     [WebMethod]
     public string ProcessRequest(XmlElement xmlDoc) {
      if(xmlDoc != null){
      return "object is not null";
      }
     return "object is null";
     }
    }
    

     

    My client's code is the following:

    namespace ClientTest
    {
     class Program
     {
     static void Main(string[] args)
     {
      WebClient webClient = new WebClient();
      Uri url = new Uri("http://localhost/wds/Demo1.asmx");
      string soap = @"<?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>
    <ProcessRequest xmlns=""urn:Demo1"">
    <foo><Id>1</Id><Name>Bar</Name></foo>
    </ProcessRequest>
    </soap:Body>
    </soap:Envelope>"
    ; webClient.Headers.Add("Content-Type", "text/xml; charset=UTF-8"); webClient.Headers.Add("SOAPAction", "urn:Demo1/ProcessRequest"); byte[] postData = Encoding.UTF8.GetBytes(soap); byte[] response = webClient.UploadData(url, "POST", postData); string res = Encoding.UTF8.GetString(response); } } }

    As a result, the response is the following:

    <?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>
    <ProcessRequestResponse xmlns="urn:Demo1">
    <ProcessRequestResult>object is null</ProcessRequestResult>
    </ProcessRequestResponse>
    </soap:Body>
    </soap:Envelope>

    That means the XmlElement's object is null. Why?

    I think ProcessRequestResult's content should be "<foo><Id>1</Id><Name>Bar</Name></foo>".

    Can you tell me where is wrong? and what should I do?

    Thanks!

     

     



    Sunday, May 08, 2011 9:04 AM

Answers

  • you need to add an xmlDoc element wrapper:

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

        <ProcessRequest xmlns="urn:Demo1">

          <xmlDoc>

            <foo>

              <Id>1</Id>

              <Name>Bar</Name>

            </foo>

          </xmlDoc>

        </ProcessRequest>

      </soap:Body>

    </soap:Envelope>


    http://webservices20.blogspot.com/
    WCF Security, Interoperability And Performance Blog
    • Proposed as answer by Yaron Naveh Sunday, May 08, 2011 1:28 PM
    • Marked as answer by Lei Zhou Sunday, May 08, 2011 3:10 PM
    Sunday, May 08, 2011 1:25 PM

All replies

  • you need to add an xmlDoc element wrapper:

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

        <ProcessRequest xmlns="urn:Demo1">

          <xmlDoc>

            <foo>

              <Id>1</Id>

              <Name>Bar</Name>

            </foo>

          </xmlDoc>

        </ProcessRequest>

      </soap:Body>

    </soap:Envelope>


    http://webservices20.blogspot.com/
    WCF Security, Interoperability And Performance Blog
    • Proposed as answer by Yaron Naveh Sunday, May 08, 2011 1:28 PM
    • Marked as answer by Lei Zhou Sunday, May 08, 2011 3:10 PM
    Sunday, May 08, 2011 1:25 PM
  • Thank you very much!

    I got it.

    Sunday, May 08, 2011 3:10 PM