我想我的XML數據轉換成JSON如何將XML數據轉換成JSON在c#
[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;
}
我用這條線,但它不適合我的話,請告訴我該怎麼辦轉換成json
this.Context.Response.ContentType = "application/json; charset=utf-8";
this.Context.Response.Write(json.Serialize(new { PersonalProfile = reader }));
輸出: -
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>
什麼XML數據你真的想轉換? –
http://stackoverflow.com/q/17398019/2746150 –
你的代碼中的xml在哪? –