Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
in web method iam returning xml string like
C#
MemoryStream ms = new MemoryStream();
      dsCustomer.WriteXml(ms, XmlWriteMode.IgnoreSchema);
      StreamReader sr = new StreamReader(ms, System.Text.Encoding.UTF8);
      ms.Position = 0;
      String strXml = sr.ReadToEnd();
      sr.Close();
      ms.Close();
      return strXml;

in success method json iam calling like
JavaScript
$.ajax({
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    url: 'url',
                    data: "{}",
                    async: false,
                    dataType: "xml",
                    success: function (data) {
                        build_menu(data)
                    },

my returned xml data from webmethod like
XML
<NewDataSet>
  <Table>
    <UserID>17727</UserID>
    <User>test user</User>
    <Ordering />
  </Table>
<Table1>
    <id>21</id>
    <CustomerID>12872</CustomerID>
    <Customer>Actavis Group</Customer>
    <Primarycsrid>16231</Primarycsrid>
    <PrimaryCSR>Kavitha Rani Aryamarati</PrimaryCSR>
    <ImplicitSameBranchAccess>true</ImplicitSameBranchAccess>
    <azGroup>A - C</azGroup>
    <IsLoggedOnUserCustomer>false</IsLoggedOnUserCustomer>
  </Table1>
</NewDataSet>

i want to show my out put as
HTML
{
  "NewDataSet": {
    "Table": [
      {
        "UserID": "17727",
        "User": "test user"
      },],
    "Table1": [
      {
        "id": "21",
        "CustomerID": "12872",
        "Customer": "Actavis Group",
        "Primarycsrid": "16231",
        "PrimaryCSR": "Kavitha Rani Aryamarati",
        "ImplicitSameBranchAccess": "true",
        "azGroup": "A - C",
        "IsLoggedOnUserCustomer": "false"
      },]
  }
}

how toonvert xml to json please help me
Posted
Updated 6-Apr-15 1:59am
v7

1 solution

Please check below link
you can use nuget package newtonsoft json[^]

and easily convert Any Collection or object to json
as generic collection , data set , data table,
make data table from xml and convert that to json .

JSON[^]


ok

Then you can use
Here dt is DataTable
C#
System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
           Dictionary<string, object> row;
           foreach (DataRow dr in dt.Rows)
           {
               row = new Dictionary<string, object>();
               foreach (DataColumn col in dt.Columns)
               {
                   row.Add(col.ColumnName, dr[col]);
               }
               rows.Add(row);
           }
           return serializer.Serialize(rows);


Use This Method
C#
private StringBuilder DatasetToJson(DataSet ds)
       {
           var sb = new StringBuilder();
           var serializer =
               new JavaScriptSerializer();
           var rows = new List<Dictionary<string, object>>();
           Dictionary<string, object> row;
           foreach (DataTable dt in ds.Tables)
           {
               foreach (DataRow dr in dt.Rows)
               {
                   row = dt.Columns.Cast<DataColumn>().ToDictionary(col => col.ColumnName, col => dr[col]);
                   rows.Add(row);
               }
               sb.Append(serializer.Serialize(rows));
           }
           return sb;
       }
 
Share this answer
 
v3
Comments
Member hani 7-Apr-15 7:50am    
third party dll will not use in my application so i have to convert through manually in json jquery
[no name] 8-Apr-15 0:47am    
Please check now solution is improved,
i add JavaScriptSerializer class functionality to convert a dataTable into searilized string like json
Member hani 8-Apr-15 0:54am    
how can i use dataset instead of datatable? along with column names..
[no name] 8-Apr-15 5:00am    
Dataset is a collection of Datatables

Dataset[0] => represent table on index 0

so you can use easily.
Member hani 8-Apr-15 7:12am    
if i give index then last added table will display but rest of tables are not save in json string

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900