Monday, 2 October 2006

Infer schema from xml data file when loading data to a dataset

In a Windows Mobile project I have a strongly typed dataset to store relational data, store the data in a xml file, and reload the data from the xml file into the dataset when application starts. One of the requirements from business needs some dynamic table to be added to the dataset. That means the dataset consists of typed, well defined tables and undefined, dynamic tables as well.

When writing data to file system, there is no problem, I simply call dataset.WriteXml(fileName) method and all tables are saved correctly. The problem comes when re-loading data from xml file to the dataset again. When I call dataset.ReadXml(fileName), only defined tables are loaded. Those dynamically generated tables are not.

When looking into some overriding methods of ReadXml, I found dataset.ReadXml(fileName, XmlReadMode.InferSchema) solved my problem. When adding this parameter, all tables are read into the dataset.

Reading more information from Microsoft online resources, I found out this relates to the concept of inference. The actual structure of the xml data itself is the basis for a relational schema. The system tries to load the xml data file into dataset and generate table structures based on the xml data itself.

No comments: