Click here to Skip to main content
12,890,879 members (54,863 online)
Rate this:
Please Sign up or sign in to vote.
See more: ASP.NET service Web

I am trying to return as json from web service .here is the code

[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
      public string GetData()
         var List = Data.ToList();
          return new JavaScriptSerializer().Serialize(List);
And my code in Client is

            type: 'Post',
            url: "http://localhost/service/WebService.asmx?op=Method",
            contentType: "application/json",
            dataType: "json",
            success: function (data) { console.log(data); },
            error: function (error, status) {
                console.log("status = " + status);
But I am getting an error

"<soap:envelope xmlns:soap="" xmlns:xsi="" xmlns:xsd=""><soap:body><soap:fault><soap:code><soap:value>soap:Receiver</soap:value></soap:code><soap:reason><soap:text xml:lang="en">System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.Xml.XmlException: Root element is missing.   at System.Xml.XmlTextReaderImpl.Throw(Exception e)   at System.Xml.XmlTextReaderImpl.ParseDocumentContent()   at System.Xml.XmlTextReaderImpl.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 ---</soap:text></soap:reason><soap:detail /></soap:fault></soap:body></soap:envelope>"

Why this happens
Posted 18-Jul-12 20:02pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

The error means what it says. The root element is missing. I guess that means the XML you're creating has more than one root. I don't know what JavaScriptSerializer is, but you may want to look at what it returns and see if it's remotely what you want.
mphotstuff 19-Jul-12 2:10am
I am passing json value from web service not xml
Christian Graus 19-Jul-12 2:22am
Do you understand what a webservice is ? Webservices communicate using XML. The XML you're trying to send is malformed. That's what the error message means. You're using a web service, therefore you're using XML. That's why your error message talks about a problem with XML.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

System.Xml.XmlException: Root element is missing.
It says all.

See if your XML file is properly formed. Make sure there is no space or new line at the top before the root tag.
Looks like stream position is not at the start of the XML making it look like root element missing.
What is there in data and how was it formed?
Christian Graus 19-Jul-12 2:22am
Really ? You can't have a space or line above the root tag ? Are you sure ?

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
Web02 | 2.8.170424.1 | Last Updated 19 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