Even though your XmlDocument is in memory you can still write it to a file
and load it much faster than if you were to compose a new DataTable out of
it.
But I will give you both solutions anyways:
1- to create a datatable from your xmlDoc:
Dim xmlDoc As New XmlDocument
'let's assume that you have an xml document that looks like this:
xmlDoc.LoadXml("<?xml version='1.0' encoding='utf-8'
?><NewDataSet><record><field name='column1'>1.1</field><field
name='column2'>1.2</field><field
name='column3'>1.3</field></record><record><field
name='column1'>2.1</field><field name='column2'>2.2</field><field
name='column3'>2.3</field></record><record><field
name='column1'>3.1</field><field name='column2'>3.2</field><field
name='column3'>3.3</field></record></NewDataSet>")
'get the root element of the xml
Dim root As XmlElement = xmlDoc.DocumentElement
'create an XPathNavigator
Dim nav As XPath.XPathNavigator = root.CreateNavigator()
'create the table definition
Dim tbl As New DataTable
Dim fieldNodes As XPath.XPathNodeIterator = nav.Select("//field")
While fieldNodes.MoveNext
Dim strFieldName As String =
fieldNodes.Current.GetAttribute("name", "")
If Not (tbl.Columns.Contains(strFieldName)) Then
tbl.Columns.Add(New DataColumn(strFieldName))
End If
End While
Dim recordNodes As XPath.XPathNodeIterator =
nav.SelectChildren(XPath.XPathNodeType.Element)
While recordNodes.MoveNext
Dim dr As DataRow = tbl.NewRow()
fieldNodes =
recordNodes.Current.SelectChildren(XPath.XPathNodeType.Element)
While fieldNodes.MoveNext
Dim strFieldName As String =
fieldNodes.Current.GetAttribute("name", "")
dr(strFieldName) = fieldNodes.Current.Value
End While
tbl.Rows.Add(dr)
End While
'rename the imported table to whichever name you want
tbl.TableName = "Errors"
ds.Tables.Add(tbl)
2- to save the xmlDoc to a disk file then retrieve it in the existing DataSet:
Dim xmlDataDoc As New XmlDataDocument
xmlDataDoc.LoadXml(xmlDoc.OuterXml)
'You need to give the ASPNET account write/modify access to this
folder
Dim w As New XmlTextWriter(Server.MapPath("App_Data\test1.xml"),
Nothing)
xmlDataDoc.WriteTo(w)
w.Close()
ds.ReadXmlSchema(Server.MapPath("App_Data\test1.xml"))
ds.ReadXml(Server.MapPath("App_Data\test1.xml"))
'rename the imported table to whichever name you want
ds.Tables(ds.Tables.Count - 1).TableName = "Errors"