Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / IIS

Xml-WebService Array Parameters

5.00/5 (1 vote)
12 Sep 2012CPOL 15.6K  
Sending Data Through XML to web Services

Introduction

This article explains how you can forward data through XML on WSDL base web services. The best part of this approach is you can forward a list of array and bytes array which enables you to forward files and images to the server using the HTTPRequest class.

Background

As we all know Visual Studio provides a pretty good option to their users to consume web services (i.e., 'Add Service Reference') which will create a client as an object in a project and the programmer can call the respective method using that client object easily, but it mostly helps other platform developers (e.g., Android, Java) if we know how others can consume our created web services.

Using the Code

So here we start. As we are working with web services, we need to write a webservice in a project.

C#
[WebMethod]
public void GetResult(int[] value)
{
      var Sample = value;
}

Now calling the above webservice method using the HttpWebRequest class:

C#
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(
    "http://localhost:3190/WebSite1/WebService.asmx">http://localhost:3190/WebSite1/WebService.asmx");
request.Method = "POST";
request.ContentType = "text/xml; charset=utf-8";

string xml = @"<?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>
    <GetResult xmlns='http://tempuri.org/'>
      <value>
        <int>5</int>
        <int>4</int>
      </value>
    </GetResult>
  </soap:Body>
</soap:Envelope>";

byte[] xmlData = System.Text.Encoding.ASCII.GetBytes(xml);
Stream str = request.GetRequestStream();
str.Write(xmlData, 0, xmlData.Length);
HttpWebResponse responce = (HttpWebResponse)request.GetResponse();

Points of Interest

The point you have to keep in your mind is that the byte[] you are sending should be endoded in 64 base binary format.

The way to write XML and request content types will be provided in your webservice details.

Image 1

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)