You must Sign In to post a response.
  • Category: Webservices

    JSON output data is wrapped in XML tag

    Hi , I am using a webservice to returm JSON Data:

    [ScriptMethod(ResponseFormat = ResponseFormat.Json,UseHttpGet =true )]

    public string GetProductListJson()
    List<ProductListMobile> list = new List<ProductListMobile>();
    list.Add(new ProductListMobile(1, "Task1", "Appnt Scheduled", 300));

    // Context.Request.Headers.Add("Content-Type", "application/json");
    // Context.Response.Headers.Add("Content-Type", "application/json");

    string s = new JavaScriptSerializer().Serialize(list);
    return s;

    following is the output in browser , when i call the service.
    <string xmlns="">
    [{ProductID: 1, Task: 'sf', TaskDescription:'sdfa'}]

    I don't want output to be wrapped in XML tags.please let me know the way.......

    Anjali Bansal
  • #768068
    Hai Anjali,
    Looks like your code is correct but there is the issue in Web.Config file.
    Make sure that your config file contains the handler to handle the request. The Web.Config file must contains the ScriptHandlerFactory under the Web.Config section for the Has below:

    <compilation debug="true" targetFramework="4.0" />
    <add name="ScriptHandlerFactory"
    verb="*" path="*.asmx"
    type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions,
    Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    resourceType="Unspecified" />

    Hope it will be helpful to you.

    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)

  • #768126
    you can use JsonConvert class, which contains helper methods for this precise purpose, check out below method

    // To convert an XML node contained in string xml into a JSON string
    XmlDocument doc = new XmlDocument();
    string jsonText = JsonConvert.SerializeXmlNode(doc);

    // To convert JSON text contained in string json into an XML node
    XmlDocument doc = JsonConvert.DeserializeXmlNode(json);

    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

Sign In to post your comments