467,132 Members | 1,145 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,132 developers. It's quick & easy.

loading Dataset from XML - urgent!

P K
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???
Feb 20 '06 #1
  • viewed: 2028
Share:
3 Replies
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()
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"P K" wrote:
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???

Feb 21 '06 #2
P K
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

"Phillip Williams" wrote:
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()
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"P K" wrote:
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???

Feb 21 '06 #3
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.XPathNode Type.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.xm l"))
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"

--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"P K" wrote:
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

"Phillip Williams" wrote:
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()
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"P K" wrote:
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???

Feb 22 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Patrick J. Schouten | last post: by
4 posts views Thread by RdS | last post: by
13 posts views Thread by Maxwell2006 | last post: by
7 posts views Thread by SteveT | last post: by
reply views Thread by Chris | last post: by
7 posts views Thread by =?Utf-8?B?TWF4R3J1dmVu?= | last post: by
3 posts views Thread by cj2 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.