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

    How to read XML Webservice response in Jquery,AngularJS

    Hi,
    I have a remote webservice which returns XML data. I want to read that XML response from webservice and convert it to JSON in Jquery.


    So request you to suggest me..
  • #762456
    Hi Jeevan,

    You can't convert XML data directly into JSON but there is some possiblities to convert into JSON with lot of other classes.

    Here's the steps to conversion of XML data into JSON,

    1. Load all of your XML data into a dataset by using readxml method

    2. Then typecast all the datarows into a generic list

    3. Using LINQ query fetch only the required detail and convert into JSON object.

    I have prepared a sample for you. Refer the below code,

    DataSet ds = new DataSet();

    string xml = @"<?xml version=""1.0"" encoding=""utf-8""?>
    <users>
    <user>
    <name>john</name>
    <age>32</age>
    </user>
    <user>
    <name>david</name>
    <age>24</age>
    </user>
    <user>
    <name>mary</name>
    <age>27</age>
    </user>
    </users>";

    ds.ReadXml(new XmlTextReader(new StringReader(xml)));

    List<DataRow> lstAllRows = ds.Tables[0].Rows.OfType<DataRow>().ToList();
    var tData = from obj in lstAllRows
    select new { Name = obj["name"], Age = obj["age"] };

    System.Web.Script.Serialization.JavaScriptSerializer oSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
    string sJsonResult = oSerializer.Serialize(tData).Trim();
    Context.Response.Write(sJsonResult);
    Context.Response.Flush();
    Context.Response.Clear();
    Context.Response.End();


    Let me know if you have any questions.

    Thanks,
    Damu

    Regards,
    V.M. Damodharan
    "Your talent will be worthless, when you have fear and tension."


  • Sign In to post your comments