none
Reading data from XML string into dataset RRS feed

  • Question

  • I am trying to populate a dataset from a xml string and bind that dataset to a gridview using the following code:

    Dim dsParameters As New DataSet
    Dim xmlData As String = "<food><fruits>apple,orange,banana</fruits></food>"
    Dim xmlSR As System.IO.StringReader = New System.IO.StringReader(xmlData)
    dsParameters.ReadXML(xmlSR, XmlReadMode.IgnoreSchema)
    Me.gvFood.DataSource = dsParameters
    Me.gvFood.DataBind()
    
    

    However, I keep getting a 'IList does not contain any data sources' error. As I step through my code, I realised that xmlSR is still null even after instantiation! Am I missing something here?

    Friday, July 30, 2010 4:36 AM

Answers

  • Is there some reason you want to populate your DataSet in this manner (reading from a string)? It can be done, but the XML string you'll  need either has to be in a different format to be able to use the .ReadXml() method, or parsed and inserted into the DataSet. If you don't actually need to get the data from an XML string, then you can simply enter your data like this (and it doesn't even need to be a DataSet, you can simply use a DataTable):

    Dim dt As DataTable = New DataTable
    dt.Columns.Add("fruits")
    Dim row As DataRow = dt.NewRow
    row("fruits") = "apple"
    dt.Rows.Add(row)
    row = dt.NewRow
    row("fruits") = "orange"
    dt.Rows.Add(row)
    row = dt.NewRow
    row("fruits") = "banana"
    dt.Rows.Add(row)
    
    Me.gvFood.DataSource = dt
    
    If you're trying to figure out how to do this from an XML string because you're going to need to do this "for real", then let me know and I'll post the correct format of XML you need to use in order to use the .ReadXml() method.
    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    Saturday, July 31, 2010 5:02 AM
  • Hi Kel,

    Try this (sorry for the C#, but it should be easy for you to follow):

    string xmlData = "<food><fruits>apple,orange,banana</fruits></food>";
    DataSet set = new DataSet();
    byte[] buffer = Encoding.UTF8.GetBytes(xmlData);
    using (MemoryStream stream = new MemoryStream(buffer))
    {
     XmlReader reader = XmlReader.Create(stream);
     set.ReadXml(reader);
    }
    
    

    Now the dataset contains a table having one row with a column named fruits. If you don't need one string value with all fruits in one row, but need a row for every fruit, the easiest way would be to modify the input xml to look like:

    "<fruits><fruit>apple</fruit><fruit>orange</fruit><fruit>banana</fruit></fruits>".

    The reading logic can remain the same.

     

     

    Marcel

     

    Saturday, July 31, 2010 7:14 AM
  • Kel,

    Reading from an XML file will have different syntax than reading an XML string. You won't need a stringreader. The look of the XML will be the same though, whether it's a string or a file, and it's similar to Marcel's example:

    <FoodDataSet>
     <FruitTable>
      <fruit>apple</fruit>
     </FruitTable>
     <FruitTable>
      <fruit>orange</fruit>
     </FruitTable>
     <FruitTable>
      <fruit>banana</fruit>
     </FruitTable>
    </FoodDataSet>
    
    

    If you created a file called Food.xml in the above format, you'd read it into your DataSet like this:

    Dim dsParameters As New DataSet
    dsParameters.ReadXML("Food.xml", XmlReadMode.InferSchema)
    
    

     


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    Monday, August 2, 2010 4:51 AM

All replies

  • Is there some reason you want to populate your DataSet in this manner (reading from a string)? It can be done, but the XML string you'll  need either has to be in a different format to be able to use the .ReadXml() method, or parsed and inserted into the DataSet. If you don't actually need to get the data from an XML string, then you can simply enter your data like this (and it doesn't even need to be a DataSet, you can simply use a DataTable):

    Dim dt As DataTable = New DataTable
    dt.Columns.Add("fruits")
    Dim row As DataRow = dt.NewRow
    row("fruits") = "apple"
    dt.Rows.Add(row)
    row = dt.NewRow
    row("fruits") = "orange"
    dt.Rows.Add(row)
    row = dt.NewRow
    row("fruits") = "banana"
    dt.Rows.Add(row)
    
    Me.gvFood.DataSource = dt
    
    If you're trying to figure out how to do this from an XML string because you're going to need to do this "for real", then let me know and I'll post the correct format of XML you need to use in order to use the .ReadXml() method.
    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    Saturday, July 31, 2010 5:02 AM
  • Hi Kel,

    Try this (sorry for the C#, but it should be easy for you to follow):

    string xmlData = "<food><fruits>apple,orange,banana</fruits></food>";
    DataSet set = new DataSet();
    byte[] buffer = Encoding.UTF8.GetBytes(xmlData);
    using (MemoryStream stream = new MemoryStream(buffer))
    {
     XmlReader reader = XmlReader.Create(stream);
     set.ReadXml(reader);
    }
    
    

    Now the dataset contains a table having one row with a column named fruits. If you don't need one string value with all fruits in one row, but need a row for every fruit, the easiest way would be to modify the input xml to look like:

    "<fruits><fruit>apple</fruit><fruit>orange</fruit><fruit>banana</fruit></fruits>".

    The reading logic can remain the same.

     

     

    Marcel

     

    Saturday, July 31, 2010 7:14 AM
  • Thanks Marcel, will try it out.

    Bonnie: Yes, I do have to read the data from a xml file. So can I have the correct xml format that you mentioned? Thanks again.

    Monday, August 2, 2010 1:26 AM
  • Kel,

    Reading from an XML file will have different syntax than reading an XML string. You won't need a stringreader. The look of the XML will be the same though, whether it's a string or a file, and it's similar to Marcel's example:

    <FoodDataSet>
     <FruitTable>
      <fruit>apple</fruit>
     </FruitTable>
     <FruitTable>
      <fruit>orange</fruit>
     </FruitTable>
     <FruitTable>
      <fruit>banana</fruit>
     </FruitTable>
    </FoodDataSet>
    
    

    If you created a file called Food.xml in the above format, you'd read it into your DataSet like this:

    Dim dsParameters As New DataSet
    dsParameters.ReadXML("Food.xml", XmlReadMode.InferSchema)
    
    

     


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    Monday, August 2, 2010 4:51 AM
  • Oh I see. Thanks.
    Friday, August 6, 2010 9:20 AM
  • You're welcome, Kel ... hope you got it all working. =0)
    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    Friday, August 6, 2010 5:07 PM