1

I want to convert my xml data to json

 [WebMethod]
 [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
 public DataTable NameArray()
 {
   DataTable imageTable = new DataTable("gcm");
   // imageTable.Columns.Add("image_name", typeof(String));
   imageTable.Columns.Add("username", typeof(String));
   imageTable.Columns.Add("gcmkey", typeof(String));
   if (con.State.ToString() == "Closed")
   {
     con.Open();
   }
   string query = "SELECT username,gcmkey from gcm";
   SqlCommand command = new SqlCommand(query, con);
   SqlDataReader reader = command.ExecuteReader();
   if (reader.HasRows)
   {
     while (reader.Read())
     {
       imageTable.Rows.Add(reader["username"],reader["gcmkey"]);
     }
   }
   reader.Close();
   con.Close();
   return imageTable;
 }

I'm using this line but it not work for me so, Please tell me what to do convert to json

this.Context.Response.ContentType = "application/json; charset=utf-8";
this.Context.Response.Write(json.Serialize(new { PersonalProfile = reader })); 

Output:-

DataTable xmlns="http://tempuri.org/">
<xs:schema xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" id="NewDataSet">
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="gcm" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="gcm">
<xs:complexType>
<xs:sequence>
<xs:element name="username" type="xs:string" minOccurs="0"/>
<xs:element name="gcmkey" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
    <diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
    <DocumentElement xmlns="">
    <gcm diffgr:id="gcm1" msdata:rowOrder="0" diffgr:hasChanges="inserted">
    <username>nirav</username>
    <gcmkey>12345</gcmkey>
    </gcm>
    </DocumentElement>
    </diffgr:diffgram>
nirav shah
  • 21
  • 1
  • 1
  • 7

2 Answers2

1

https://stackoverflow.com/a/814027/5923666 Someone already asked that but I'll give you the answer...

Yes. Using the JsonConvert class which contains helper methods for this precise purpose:

// 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);

Documentation here: Converting between JSON and XML with Json.NET

Community
  • 1
  • 1
Raz Luvaton
  • 3,166
  • 4
  • 21
  • 36
1

Include Newtonsoft.Json from Nuget
And use this

JsonConvert.SerializeObject(dt, Newtonsoft.Json.Formatting.Indented);

Output will be something like this :

{ "id": "1", "name": "Mr. xyz", "Age": "25", "Country": "India", "Address": "H no- 4561", "Phone": "1258961" }, { "id": "2", "name": "Mr. xyz", "Age": "26", "Country": "India", "Address": "H no- 4562", "Phone": "1258962" }, { "id": "3", "name": "Mr. xyz", "Age": "27", "Country": "India", "Address": "H no- 4563", "Phone": "1258963" }, { "id": "4", "name": "Mr. xyz", "Age": "28", "Country": "India", "Address": "H no- 4564", "Phone": "1258964" }, { "id": "5", "name": "Mr. xyz", "Age": "29", "Country": "India", "Address": "H no- 4565", "Phone": "1258965" }

Let me know if it serve your purpose but you feel difficulty to put it together

Saurabh
  • 1,505
  • 6
  • 20
  • 37