getting an XML STRING into a dataset?

Discussion in 'ASP .Net' started by darrel, May 26, 2006.

  1. darrel

    darrel Guest

    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
    darrel, May 26, 2006
    #1
    1. Advertising

  2. darrel

    sloan Guest

    dim ds as DataSet = new DataSet


    "darrel" <> wrote in message
    news:...
    > 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
    >
    >
    sloan, May 26, 2006
    #2
    1. Advertising

  3. darrel

    darrel Guest

    > 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
    darrel, May 26, 2006
    #3
  4. darrel

    sloan Guest

    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;
    }







    "darrel" <> wrote in message
    news:%...
    > > 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
    >
    >
    sloan, May 26, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nedu N
    Replies:
    1
    Views:
    1,792
    Chris Taylor
    Oct 31, 2003
  2. Nedu N
    Replies:
    2
    Views:
    2,150
  3. Replies:
    0
    Views:
    431
  4. Nedu N
    Replies:
    2
    Views:
    219
    Jacob Yang [MSFT]
    Oct 31, 2003
  5. John

    Getting web service dataset into xml

    John, Jun 25, 2004, in forum: ASP .Net Web Services
    Replies:
    4
    Views:
    136
    Kondratyev Denis
    Jun 26, 2004
Loading...

Share This Page