Click here to Skip to main content
15,906,341 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:

Hiii friends i want to create a web service in asp.net which request and response in xml data.
If any body can give me a sample code which request as well as response the xml data please give me.I want to complete my mca final semester project as soon as.
Posted
Comments
AlexDpars 27-Dec-13 9:59am    
Google is your friend... How to make a webservice .net (try it ;))
pkvartej7121 27-Dec-13 10:14am    
Mr AlexDpars i searching on google since last 5 to 6 hours i want any simple example i dont know about it very much.
gettgotcha 27-Dec-13 10:06am    
http://msdn.microsoft.com/en-us/library/7bkzywba(v=vs.80).aspx
pkvartej7121 27-Dec-13 10:15am    
Thanx sir but i can not understand it properly if you have any example please give me
AlexDpars 27-Dec-13 10:25am    
This one is pretty simple but require you to use VS 2005... I don't think you can do this with VS 2008 and newer versions... http://www.codeproject.com/Articles/863/Your-first-C-Web-Service

1 solution

Here is a generic code sample of a service I wrote. It uses SOAP to contact the service and the service returns XML.

I use a stored procedure to handle the data that is passed to the service so that it can be used for multiple purposes.

ISercie.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Data;
namespace MyFirstService
{
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        DataTable MyService(string storedProcedure, string data);
    }
}


Service1.svc.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.Xml.Serialization;
using System.IO;
namespace MyFirstService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    public class Service1 : IService1
    {
       public DataTable MyService(string storedProcedure, string data)
       {
           string connString = ConfigurationManager.ConnectionStrings["MyServiceConnString"].ConnectionString;
           SqlConnection conn = new SqlConnection(connString);
           SqlCommand cmd = new SqlCommand(storedProcedure, conn);
           SqlDataReader reader = null;
           cmd.CommandType = CommandType.StoredProcedure;
           cmd.Parameters.AddWithValue("@data", data);
           try
           {
               conn.Open();
               reader = cmd.ExecuteReader();         
               //load the datatable with the SqlDataReader
               DataTable dataTable = new DataTable();
               dataTable.TableName = "ReturnData";
               dataTable.Load(reader);
               //convert the datatable to xml
               StringWriter xmlWriter = new StringWriter();
               dataTable.WriteXml(xmlWriter, XmlWriteMode.IgnoreSchema, false);
               conn.Close();
               return dataTable;
           }
           catch (Exception)
           {
               throw;
           }
       }
    }
}


VB Script with the SOAP call
Dim soapServer, soapMessage, data

  For x = 1 To Request.Form.Count()
      fieldName = Request.Form.Key(x)
      fieldValue = Request.Form.Item(x)
      If fieldName <> "Submit" And fieldName <> "Password" Then
          data = data & fieldName & "~" & Replace(Replace(Replace(fieldValue, "~", ""), "|", ""), "&", "and") & "|"
      End If
  Next


  'service location - Production
   soapServer = "http://locationofyourservice/MyFirstService/Service1.svc"


  'message including soap envelope wrapper, to send to the service
  soapMessage = "<s:Envelope xmlns:s=" & Chr(34) & "http://schemas.xmlsoap.org/soap/envelope/" & Chr(34) & ">" & _
                      "<s:Body>" & _
                          "<MyService xmlns=" & Chr(34) & "http://tempuri.org/" & Chr(34) & ">" & _
                              "<storedProcedure>" & storedProcedure & "</storedProcedure>"  & _
                              "<data>" & data & "</data>" & _
                          "</MyService>" & _
                      "</s:Body>" & _
                  "</s:Envelope>"

  Set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP")

  xmlhttp.open "POST", soapServer, False
  xmlhttp.setRequestHeader "Man", POST & " " & soapServer & " HTTP/1.1"
   xmlhttp.setRequestHeader "SOAPAction", "http://tempuri.org/IService1/MyService"

  xmlhttp.setRequestHeader "Content-Type", "text/xml; charset=utf-8"

  'Calling the service'
  xmlhttp.send(soapMessage)

  Response.Write xmlhttp.responseText
 
Share this answer
 
v2
Comments
pkvartej7121 28-Dec-13 9:21am    
Thank you Richard very much.
Richard C Bishop 30-Dec-13 10:03am    
You are welcome, come back anytime.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900