473,411 Members | 1,949 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

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
3 2248
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Patrick J. Schouten | last post by:
I am trying to transfor an XML document prior to loading into a Dataset. My problem stems from the known bug in Visual Studio that prevents loading a well formed XML because of duplicate child...
4
by: RdS | last post by:
hello, When I create a form that ties to dataset of table with only 20 records in it I understand that all 20 records are copied to my local computers memory for manipulation. Then if rows were...
13
by: Maxwell2006 | last post by:
Hi, We are having a debate over using DataSet as return value type for web services. The problem is that we don't know whether Java applications can use DataSet
7
by: SteveT | last post by:
Can someone point me in the right direction? Somewhere I read that you reference a strongly typed dataset as if it were a class structure. For example, <SomeTests> <TestsGroups> <Group>...
7
by: koonda | last post by:
Hi guys, I am trying to create a web interface in C# using ASP.NET. The database being used is SQL Server. I have some problems loading the tables in the datalist controls. When I run the program...
1
by: | last post by:
I'm querying Index Server to return search results, both regular properties and some custom properties I've created. Index Server has this preference for thinking about information as strings...
0
by: Chris | last post by:
Hello, I have a problem with re-loading datasets. As a simple example, if I have an SQL table of addresses comprising active and inactive addresss, I wish to load either sub-set by clicking on...
7
by: =?Utf-8?B?TWF4R3J1dmVu?= | last post by:
I have a DLL that implements the Business and Data Layers for a number of different websites. That DLL uses a Strongly Typed DataSet to interface to the Data Source which is SQL Server. There...
3
by: cj2 | last post by:
I can't remember and can't find my notes anywhere. There were a couple of commands that when used with I think the data adapter that allowed for faster reading or writing of tables. I think one...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.