How to serialize a DataTable to a string?

前端 未结 4 1319
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-15 08:47

Recently I was in the need to serialize a DataTable as a string for further processing (storing in a file).

So I asked myself: How to serialize a DataTa

4条回答
  •  长情又很酷
    2020-12-15 09:42

    You can also try writing out the DataTable to XML which works just as nicely:

    Dim dt As DataTable
    Dim DataTableAsXMLString As String
    '...code to populate DataTable
    Using sw As New StringWriter()       
        dt.WriteXml(sw)
        DataTableAsXMLString = sw.ToString()
    End Using
    

    ...then if needed you can convert the XML right back to a DataTable:

    Dim ds As New DataSet
    Dim dt2 As DataTable
    Using sr As New StringReader(DataTableAsXMLString)
        ds.ReadXml(sr)
        dt2 = ds.Tables(0)
    End Using
    

提交回复
热议问题