how to serialize a DataTable to json or xml

后端 未结 3 1149
情歌与酒
情歌与酒 2021-01-12 04:41

i\'m trying to serialize DataTable to Json or XML. is it possibly and how? any tutorials and ideas, please.

For example a have a sql table:

CREATE          


        
3条回答
  •  臣服心动
    2021-01-12 05:32

    Here is how to convert it to Json:

            DataTable dt = new DataTable();
            dt.Load(reader);
            string temp = JsonConvert.SerializeObject(dt);
    

    and if you want to convert this json to a list of objects (it could be your EF table) then use the below:

    dbContext db = new dbContext();
    List jsonList = (List)JsonConvert.DeserializeObject(temp, typeof(List));
    
        

    提交回复
    热议问题