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:

    [System.Web.Services.WebMethod()]
    [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="http://tempuri.org/">
    [{ProductID: 1, Task: 'sf', TaskDescription:'sdfa'}]
    </string>

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

    Thanks!
    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:

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

    Hope it will be helpful to you.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com

  • #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();
    doc.LoadXml(xml);
    string jsonText = JsonConvert.SerializeXmlNode(doc);

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

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


  • Sign In to post your comments