getting an XML STRING into a dataset?

D

darrel

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
 
D

darrel

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
 
S

sloan

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.ToString(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;
}
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top