How I do serialize the data table into XML?

Dec 22 2022 4:32 AM


I have an issue while convert data table into XML

Error: Cannot serialize the DataTable. DataTable name is not set.

What I have tried :

    Public Function ConvertDatatableToXML(ByVal dt As DataTable) As String
        Dim str As MemoryStream = New MemoryStream()
        dt.WriteXml(str, True)
        str.Seek(0, SeekOrigin.Begin)
        Dim sr As StreamReader = New StreamReader(str)
        Dim xmlstr As String
        xmlstr = sr.ReadToEnd()
        Return (xmlstr)
    End Function 

    Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
        Dim dt As DataTable = New DataTable()
        dt = GetData()
        Dim XMLstring As String = clsAPI.ConvertDatatableToXML(dt)
        Me.txtComments.Text = XMLstring

    End Sub

   Public Function GetData() As DataTable
        Dim dt As New DataTable
        Dim adp As New SqlDataAdapter("usp_TR_Data", conn)
        adp.SelectCommand.CommandType = CommandType.StoredProcedure
        adp.SelectCommand.CommandTimeout = 150

        With adp.SelectCommand.Parameters
            .Add("@Action", SqlDbType.NVarChar).Value = "CONVERT-XML"
            .Add("@SONo", SqlDbType.NVarChar).Value = Me.txtSONo.Text
        End With
        Return dt
    End Function

SQL Query
            [dairport],[scountry],    [detailOfContent],[weight],[productPackages],
        From TR_Data
        where SONo=@sono    


pls advice me

Thank  you


Answers (2)