473,394 Members | 1,737 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,394 software developers and data experts.

getting an XML STRING into a dataset?

I'm grabbing an XML string from a database and trying to pass it into a
dataset.

I've read this article:
http://www.aspfaqs.com/aspfaqs/ShowFAQ.asp?FAQID=213

and have tried to implement it as such:

Dim strRdr As StringReader
strRdr = New StringReader(DS.Tables(0).Rows(0)("xml").ToString)
dim dsMenu as dataset
dsMenu.ReadXml(strRdr)

However, the last line keeps giving me a "Object reference not set to an
instance of an object" and I don't know why. Anyone see anything wrong with
my code?

-Darrel
May 26 '06 #1
3 1728


dim ds as DataSet = new DataSet
"darrel" <no*****@nowhere.com> wrote in message
news:eB**************@TK2MSFTNGP03.phx.gbl...
I'm grabbing an XML string from a database and trying to pass it into a
dataset.

I've read this article:
http://www.aspfaqs.com/aspfaqs/ShowFAQ.asp?FAQID=213

and have tried to implement it as such:

Dim strRdr As StringReader
strRdr = New StringReader(DS.Tables(0).Rows(0)("xml").ToString)
dim dsMenu as dataset
dsMenu.ReadXml(strRdr)

However, the last line keeps giving me a "Object reference not set to an
instance of an object" and I don't know why. Anyone see anything wrong with my code?

-Darrel

May 26 '06 #2
> dim ds as DataSet = new DataSet

Oops. Had a typo in the last email.

This is what I originally tried:

---------------

Dim strRdr As StringReader = New
StringReader(DS.Tables(0).Rows(0)("xml").ToString)

theDStoReturn.dsMenu.ReadXml(strRdr)

---------------

and finally got it working with this:

---------------
Dim strRdr As StringReader = New
StringReader(DS.Tables(0).Rows(0)("xml").ToString)

Dim ds2 As DataSet = New DataSet
ds2.ReadXml(strRdr)

theDStoReturn.dsMenu = ds7
---------------

I'm not sure why I need that interim DS, though.

-Darrel
May 26 '06 #3
This might help.

This is what I use when I need a quick DataSet for testing. I NEVER put
this into production, but might offer a clue for you:
Private Function GetDataSet1() As DataSet
Dim ds As DataSet = New DataSet

Dim sb As New System.Text.StringBuilder
sb.Append("<?xml version=""1.0""?><items>")
'sb.Append("<item>")
'sb.Append("<itemid>0</itemid>")
'sb.Append("<friendlyname1>Food</friendlyname1>")
'sb.Append("<friendlyname2>Food</friendlyname2>")
'sb.Append(("<time>" + DateTime.Now.ToLongTimeString() + "</time>"))
''sb.Append("<parentid></parentid>")
'sb.Append("</item>")
sb.Append("<item>")
sb.Append("<itemid>100</itemid>")
sb.Append("<friendlyname1>Vegetables</friendlyname1>")
sb.Append("<friendlyname2>VG</friendlyname2>")
sb.Append(("<time>" + DateTime.Now.ToLongTimeString() + "</time>"))
'sb.Append("<parentid>0</parentid>")
sb.Append("</item>")
sb.Append("<item>")
sb.Append("<itemid>200</itemid>")
sb.Append("<friendlyname1>Fruits</friendlyname1>")
sb.Append("<friendlyname2>FR</friendlyname2>")
sb.Append(("<time>" + DateTime.Now.ToLongTimeString() + "</time>"))
'sb.Append("<parentid>0</parentid>")
sb.Append("</item>")

sb.Append("<item>")
sb.Append("<itemid>1001</itemid>")
sb.Append("<friendlyname1>Potatoes</friendlyname1>")
sb.Append("<friendlyname2>PO</friendlyname2>")
sb.Append(("<time>" + DateTime.Now.ToLongTimeString() + "</time>"))
sb.Append("<parentid>100</parentid>")
sb.Append("</item>")

sb.Append("<item>")
sb.Append("<itemid>1002</itemid>")
sb.Append("<friendlyname1>Spinach</friendlyname1>")
sb.Append("<friendlyname2>SP</friendlyname2>")
sb.Append(("<time>" + DateTime.Now.ToLongTimeString() + "</time>"))
sb.Append("<parentid>100</parentid>")
sb.Append("</item>")

sb.Append("<item>")
sb.Append("<itemid>2001</itemid>")
sb.Append("<friendlyname1>Apples</friendlyname1>")
sb.Append("<friendlyname2>AP</friendlyname2>")
sb.Append(("<time>" + DateTime.Now.ToLongTimeString() + "</time>"))
sb.Append("<parentid>200</parentid>")
sb.Append("</item>")

sb.Append("<item>")
sb.Append("<itemid>2002</itemid>")
sb.Append("<friendlyname1>Bananas</friendlyname1>")
sb.Append("<friendlyname2>BN</friendlyname2>")
sb.Append(("<time>" + DateTime.Now.ToLongTimeString() + "</time>"))
sb.Append("<parentid>200</parentid>")
sb.Append("</item>")

sb.Append("<item>")
sb.Append("<itemid>20001</itemid>")
sb.Append("<friendlyname1>Granny Smith</friendlyname1>")
sb.Append("<friendlyname2>GS</friendlyname2>")
sb.Append(("<time>" + DateTime.Now.ToLongTimeString() + "</time>"))
sb.Append("<parentid>2001</parentid>")
sb.Append("</item>")
sb.Append("<item>")

sb.Append("<itemid>20002</itemid>")
sb.Append("<friendlyname1>Macintosh</friendlyname1>")
sb.Append("<friendlyname2>MA</friendlyname2>")
sb.Append(("<time>" + DateTime.Now.ToLongTimeString() + "</time>"))
sb.Append("<parentid>2001</parentid>")
sb.Append("</item>")

sb.Append("</items>")

Dim ms As New System.IO.MemoryStream
Dim writer As New System.IO.StreamWriter(ms)
writer.Write(sb.ToString())
writer.Flush()
ms.Position = 0
ds.ReadXml(ms)
Return ds
End Function 'GetDataSet1


private static EmpDataDS GetDataSet1()
{

EmpDataDS ds = new EmpDataDS();
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("<?xml version=\"1.0\"?><EmpDataDS>");
sb.Append("<Emp>");
sb.Append("<EmpID>123</EmpID>");
sb.Append("<LastName>Smith</LastName>");
sb.Append("<FirstName>John</FirstName>");
sb.Append("<SSN>222-22-2222</SSN>");
sb.Append("<DateOfBirth>" + DateTime.Now.ToLongDateString() +
"</DateOfBirth>");
sb.Append("<Litho1>litho123</Litho1>");
sb.Append("<Litho2>litho1 3</Litho2>");
sb.Append("</Emp>");
sb.Append("<Emp>");
sb.Append("<EmpID>456</EmpID>");
sb.Append("<LastName>Jones</LastName>");
sb.Append("<FirstName>Mary</FirstName>");
sb.Append("<SSN>333-33-3333</SSN>");
sb.Append("<DateOfBirth>01/01/1972</DateOfBirth>");
sb.Append("</Emp>");
for(int i = 0; i < 21 ; i++)
{

sb.Append("<Emp>");
sb.Append("<EmpID>" + (i+1000) + "</EmpID>");
sb.Append("<LastName>"+"EmpLastName"+"</LastName>");

sb.Append("<FirstName>"+"EmpFirstName:"+Convert.To String(i)+"</FirstName>");
sb.Append("<SSN>" + Convert.ToString(( Convert.ToString(i+1000) +
"000000000" )).Substring(0,9) + "</SSN>");
sb.Append("<DateOfBirth>01/01/1972</DateOfBirth>");
sb.Append("</Emp>");
}

sb.Append("</EmpDataDS>");
System.IO.MemoryStream ms = new System.IO.MemoryStream();
System.IO.StreamWriter writer = new System.IO.StreamWriter(ms);
writer.Write(sb.ToString());
writer.Flush();
ms.Position = 0;
ds.ReadXml(ms);
return ds;
}



"darrel" <no*****@nowhere.com> wrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
dim ds as DataSet = new DataSet


Oops. Had a typo in the last email.

This is what I originally tried:

---------------

Dim strRdr As StringReader = New
StringReader(DS.Tables(0).Rows(0)("xml").ToString)

theDStoReturn.dsMenu.ReadXml(strRdr)

---------------

and finally got it working with this:

---------------
Dim strRdr As StringReader = New
StringReader(DS.Tables(0).Rows(0)("xml").ToString)

Dim ds2 As DataSet = New DataSet
ds2.ReadXml(strRdr)

theDStoReturn.dsMenu = ds7
---------------

I'm not sure why I need that interim DS, though.

-Darrel

May 26 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Filippo Pandiani | last post by:
I have a grid that shows the file list from a folder. On the postback, how do I get a Dataset from this grid? Thanks, Filippo.
6
by: Erik H. | last post by:
Trying to connect to MySQL db on localhost, and populate datagrid from a dataset using code inline method. Getting the following compile error: Error Message: "CS0246: The type or namespace...
7
by: gemel | last post by:
I am developing an application that uses SQL 2000 as my source of images. I have successfully created the code to load the images onto the SQL Server and also to retrieve the images into a dataset....
1
by: Nathan Sokalski | last post by:
I have retrieved data from a database using a SELECT statement that includes an INNER JOIN. The data seems to be retrieved to the DataSet OK, but I am having trouble getting the data from the...
13
by: dbuchanan | last post by:
Hello, Here is the error message; ---------------------------- Exception Message: ForeignKeyConstraint Lkp_tbl040Cmpt_lkp302SensorType requires the child key values (5) to exist in the...
1
by: momo | last post by:
Can some take a look at this and tell me what I am doing wrong and how to fix it. I an new to ASP.NET Getting Name "GetMsAccessOldebConnection' is not declared " Error <%@ Page...
1
by: Jerry John | last post by:
I am working in ASP.NET with C#. I have a text file which contains datas with delimiters. For example:- MSH|^~\$|DISCHARGE|CLAY COUNTY MEMORIAL|||200502110939| I also have an XML file created...
0
by: mike1402 | last post by:
Hi ! I get the error below sometimes when retrieving a big amount of data using Datadapter.Fill(dataset,"table"). But when I send the command Fill again, there is no error. Is it a fault of...
2
by: rocketfire97 | last post by:
I'm trying to call a COM object using C# but having no luck getting values back for passed in ref objects. I've tried the same call using VB.NET and can get data back. How would I implement the...
2
by: Andrew Cooper | last post by:
Greetings, I've got a website that has several pages with DataGrid controls on them. The controls are bound to Object Datasources. On one of the pages I keep getting a "Timeout expired. The...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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
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.