Function SerializeDatatable(ByVal oDT As DataTable, ByVal strDataTableName As String) As String
'declare objects
Dim mem As New MemoryStream
Dim settings As New XmlWriterSettings
settings.Encoding = New UTF8Encoding(False)
Dim tw As New XmlTextWriter(mem, Encoding.UTF8)
'name the datatable and dataset.
'this will cause the XML to generate like this:
'<?xml version="1.0" encoding="utf-16"?>
'<Root>
' <strDataTableName>
oDT.TableName = strDataTableName
oDT.DataSet.DataSetName = "Root"
'initialize
tw.Formatting = Formatting.Indented
'start the document
tw.WriteStartDocument()
'insert the datatable
oDT.WriteXml(tw)
'close the document
tw.WriteEndDocument()
tw.Flush()
tw.Close()
Dim result As String = Encoding.UTF8.GetString(mem.ToArray())
Return result
End Function