Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > getting an XML STRING into a dataset?

Reply
Thread Tools

getting an XML STRING into a dataset?

 
 
darrel
Guest
Posts: n/a
 
      05-26-2006
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


 
Reply With Quote
 
 
 
 
sloan
Guest
Posts: n/a
 
      05-26-2006


dim ds as DataSet = new DataSet


"darrel" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
 
 
 
darrel
Guest
Posts: n/a
 
      05-26-2006
> 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


 
Reply With Quote
 
sloan
Guest
Posts: n/a
 
      05-26-2006
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" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> > 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
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ElementTree.XML(string XML) and ElementTree.fromstring(string XML)not working Kee Nethery Python 12 06-27-2009 06:06 AM
Problem to insert an XML-element by XSLT-converting from one XML-file into another XML-file jkflens XML 2 05-30-2006 09:41 AM
Different results parsing a XML file with XML::Simple (XML::Sax vs. XML::Parser) Erik Wasser Perl Misc 5 03-05-2006 10:09 PM
Tools to extract data from SQL database and convert it into XML & insert XML data into SQL databases Harry Zoroc XML 1 07-12-2004 10:10 PM
how to write some xml into huge xml file into speceific location??? Xenia XML 4 11-25-2003 07:14 PM



Advertisments