Use XML to fill Dataset PDF Print E-mail
Written by Joris Bijvoets   
Thursday, 19 November 2009 21:57

How to use XML to fill a Dataset in Visual Basic.Net?

Datasets are great. As soon as you discover te power of having one or more datasets in memory, query them etcetera, you'll start to use them as much as possible.

Lately I used them to connect Microsoft Charts with data. The data is read first from several different data formats (CSV, text files, MS Excel and Postgres) into a dataset. After this import has taken place, it's possible to draw charts while combining data from several sources.

I have choosen to serialize the dataset as XML, and to serialize the structure of the dataset as XSD. The XML and XSD are stored in a ntext-field in Sql Server.

To rebuild the dataset would be easy this way, I thought. Just retrieve the XML and XSD from the database, and read both back into a new dataset, using the functions Dataset.ReadXmlSchema and Dataset.ReadXML. Nothing wrong with this logic.

However, I encountered a problem: the saved structure and data where given back to the application as a string. The ReadXmlSchema and ReadXML methods only accept a file or a stream as parameter. My question changed into the following?

How to convert this XMLSchema and XML from string to stream?

I used the function System.IO.StringReader. The code reads like this:

  Dim DS as new Dataset
  Dim sXmlschema as string 'Fill with the XMLSchema to use
  Dim sXML as string ' Fill with the XML Data to use
  DS.ReadXmlSchema(New System.IO.StringReader(sXMLschema)
  DS.ReadXml(New System.IO.StringReader(sXML)

 

Last Updated on Wednesday, 06 January 2010 06:21