loading Dataset from XML - urgent!

G

Guest

Hello,

I have a dataset which is already loaded with data in one table.
so dataset.tables("data") exists and has data.

Now, I have to add another table to the dataset. The source for this table
is a XMLDoc. So I have the XML in a XMLDoc and I need to create a table in
the dataset which would read xml from the XMLDoc .

So in essence I need something like
dataset.tables("error").readXML = "<xml><r1></r1></xml>"


How can this be done???
 
G

Guest

Dim ds As New DataSet
'load the first table from an xml file
ds.InferXmlSchema(Server.MapPath("xmlfile1.xml"), Nothing)
ds.ReadXml(Server.MapPath("xmlfile1.xml"))
'load another table from an xml file
ds.InferXmlSchema(Server.MapPath("xmlfile2.xml"), Nothing)
ds.ReadXml(Server.MapPath("xmlfile2.xml"))
'databind one datagrid to the first table
datagrid1.DataSource = ds.Tables(0)
datagrid1.DataBind()
'databind another datagrid to the second table
datagrid2.DataSource = ds.Tables(1)
datagrid2.DataBind()
 
G

Guest

Phillip,

I cannot do what you say, cosI already have a dataset with a table in it.
Now I need to add a second table to it using an XML Dom object.
My XML does not reside in a file. It is in an XML DOM object.

So the goal is to add a new table to an existing dataset and the new
datatable should be loaded using an XML DOM object.

thanks
PK
 
G

Guest

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"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top