Click here to Skip to main content
12,697,294 members (27,869 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# ASP XML WebService
Hi friends,
I am calling webservice from classic asp.
and i am able to call the service but not able to get the correct output.
i am geting following error.
soap:ReceiverSystem.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.Xml.XmlException: Data at the root level is invalid. Line 1, position 1. at System.Xml.XmlTextReaderImpl.Throw(Exception e) at System.Xml.XmlTextReaderImpl.Throw(String res, String arg) at System.Xml.XmlTextReaderImpl.ParseRootLevelWhitespace() at System.Xml.XmlTextReaderImpl.ParseDocumentContent() at System.Xml.XmlTextReaderImpl.Read() at System.Xml.XmlTextReader.Read() at System.Web.Services.Protocols.SoapServerProtocol.SoapEnvelopeReader.Read() at System.Xml.XmlReader.MoveToContent() at System.Web.Services.Protocols.SoapServerProtocol.SoapEnvelopeReader.MoveToContent() at System.Web.Services.Protocols.SoapServerProtocolHelper.GetRequestElement() at System.Web.Services.Protocols.Soap12ServerProtocolHelper.RouteRequest() at System.Web.Services.Protocols.SoapServerProtocol.RouteRequest(SoapServerMessage message) at System.Web.Services.Protocols.SoapServerProtocol.Initialize() at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean& abortProcessing) --- End of inner exception stack trace --- 

below is my asp code to call websrvice..

If Request.ServerVariables("REQUEST_METHOD") = "POST" Then
Dim xmlhttp
Dim DataToSend
Dim postUrl
If Request.Form.Item("Operation")="Sum" Then
postUrl = ""
postUrl = "" 
end if 
'set xmlhttp = Server.CreateObject("msxml2.ServerXMLHTTP") 
'set xmlhttp = Server.CreateObject("Microsoft.XMLHTTP") 
'set xmlhttp = Server.CreateObject("MSXML2.DOMDocument")
'Set xmlhttp = Server.CreateObject("MSXML2.ServerXMLHTTP.4.0") 

Set xmlhttp = server.Createobject("MSXML2.XMLHTTP")
xmlhttp.Open "POST",postUrl,false
xmlhttp.setRequestHeader "Content-Type","application/x-www-form-urlencoded"
'Response.Write postUrl
xmlhttp.send DataToSend

Response.Write DataToSend & "<br>"
Response.Write(xmlhttp.responseText )
Response.Write "Loading for first Time" 
End If

and in my webservice there are only two webmethods. sum & below
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;

namespace WebService1
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class Service1 : System.Web.Services.WebService

        public string Sum(int val1, int val2)
            return "The Sum of two number= " + (val1 + val2);
        public string Subtract(int val1, int val2)
            return "The Subtraction of two number= " + ((val1 > val2) ? (val1 - val2) : (val2 - val1));


your help would be appreciable..
Posted 9-Jul-12 19:21pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

System.Xml.XmlException: Data at the root level is invalid. Line 1, position 1
See if your XML file is properly formed. Make sure there is no space or new line at the top before the root tag.

Stack trace says, webservice was expecting request in form of a valid XML but request did not had a valid xml.

Did you DEBUG and see what was sent? Fiddler?

Sending a method as a querystring and using XMLHttpRequest would work only for methods with no parameters.
For accessing methods with parameters, you need to pass the URL with method name and paratemer as query string.
Following article illustrates it: Consuming .NET Web Services Using the XMLHTTP Protocol[^]

Here have a look at this another article on similar scenario: Calling a WebService from ASP3.0 and JavaScript[^]
ssd_coolguy 10-Jul-12 2:50am
thanks for reply... your links helped me lot... :)
i am about to solve this problem..:) soon i will update answer here..
Sandeep Mewara 10-Jul-12 5:48am
Good to know.

Prasad_Kulkarni 10-Jul-12 6:36am
Good answer! +5
Sandeep Mewara 13-Jul-12 3:27am
Thanks PK.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Hello friends,
i have solved this issue...
please see my code for SUm method..

   Dim xmlhttp
   Dim DataToSend
   Dim postUrl
   postUrl = ""
  Set xmlhttp = server.Createobject("MSXML2.XMLHTTP")
   xmlhttp.Open "POST",postUrl,false
   xmlhttp.setRequestHeader "Content-Type","application/x-www-form-urlencoded"
   xmlhttp.send DataToSend

and the main thing is below code needs to add in webservice's web.confg file.

        <add name="HttpGet" />
        <add name="HttpPost" />

Thanks for all replys... :)

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170118.1 | Last Updated 10 Jul 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100