以下代码段实现从DataTable到XML数据的转化过程:
1
Public Function ConvertDataTableToXML()Function ConvertDataTableToXML(ByVal strTableName As String, ByVal dt As DataTable) As String
2
3
Dim xmldoc As New XmlDocument()
4
5
Dim strXML As String = String.Empty
6
Dim sb As New StringBuilder
7
Dim sw As New IO.StringWriter(sb)
8
dt.WriteXml(sw)
9
sw.Close()
10
xmldoc.InnerXml = sb.ToString
11
12
strXML = "<" & strTableName & ">"
13
If Not xmldoc.FirstChild Is Nothing AndAlso _
14
Not xmldoc.FirstChild.FirstChild Is Nothing Then
15
strXML = strXML & xmldoc.FirstChild.FirstChild.InnerXml
16
End If
17
strXML = strXML & "</" & strTableName & ">"
18
19
strXML = strXML.Replace("<", "<

Public Function ConvertDataTableToXML()Function ConvertDataTableToXML(ByVal strTableName As String, ByVal dt As DataTable) As String2

3
Dim xmldoc As New XmlDocument()4

5
Dim strXML As String = String.Empty6
Dim sb As New StringBuilder7
Dim sw As New IO.StringWriter(sb)8
dt.WriteXml(sw)9
sw.Close()10
xmldoc.InnerXml = sb.ToString11

12
strXML = "<" & strTableName & ">"13
If Not xmldoc.FirstChild Is Nothing AndAlso _14
Not xmldoc.FirstChild.FirstChild Is Nothing Then15
strXML = strXML & xmldoc.FirstChild.FirstChild.InnerXml16
End If17
strXML = strXML & "</" & strTableName & ">"18

19
strXML = strXML.Replace("<", "<
