我試圖將DataTable序列化爲Json或XML。它可能和如何?請提供任何教程和想法。如何將DataTable序列化爲json或xml
例如,一個有一個SQL表:
CREATE TABLE [dbo].[dictTable](
[keyValue] [int] IDENTITY(1,1) NOT NULL,
[valueValue] [int] NULL,
CONSTRAINT [Psd2Id] PRIMARY KEY CLUSTERED
(
[keyValue] ASC
)WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
C#代碼:
string connectionString =
"server=localhost;database=dbd;uid=**;pwd=**";
SqlConnection mySqlConnection = new SqlConnection(connectionString);
string selectString = "SELECT keyValue, valueValue FROM dicTable";
SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
mySqlCommand.CommandText = selectString;
SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();
mySqlDataAdapter.SelectCommand = mySqlCommand;
DataSet myDataSet = new DataSet();
mySqlConnection.Open();
string dataTableName = "dictionary";
mySqlDataAdapter.Fill(myDataSet, dataTableName);
DataTable myDataTable = myDataSet.Tables[dataTableName];
//now how to serialize it?