Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to create a typed dataset at run-time without .XSD files? 1

Status
Not open for further replies.

huckfinn19

Programmer
May 16, 2003
90
CA
Hi!

My goal is to create a typed dataset from an XML file, meaning that I don't want all columns to be strings, but also integers, floats and so on. The thing is that at run-time, I know about the types of the fields to be created, but I don't have this information in a .xsd or .dtd file and don't want to use schemas in other files.

So here is an example XML file:


<?xml version="1.0" encoding="utf-8" ?>
<config xmlns=" <params>
<SomeFloat>0.25</SomeFloat>
<SomeInt>25</SomeInt>
<SomeString>A string</SomeString>
</params>
</config>

I know at run-time that SomeFloat is a float, SomeInt an int and son on. I want to load the XML with somthing like :

----------
m_Dataset = new DataSet();
m_Dataset.ReadXml("simple.xml");
//Linking dataset to datasource
DataGrid datagrid = new DataGrid();
dataGrid1.DataSource = m_Dataset;
//Selecting the node that is of interest
dataGrid1.DataMember = "params";
dataGrid1.CaptionText = dataGrid1.DataMember;
----------

How can I make sure that the type of the columns won't be only strings? How can I specify these types? Should I create the table in the dataset first?

Thanks!

Huck
 
If you have no need for strong typing in your code, you can just leave your code as is so long as simple.xml properly declares the datatypes and structure of your dataset. Even though you might do this:

Code:
int intSomeInt = (int) m_Dataset.Tables["tblMyTable"].Rows[0]["SomeInt"];

MyColumn will be the datatype declared in simple.xml.

However, if you need this in code:
Code:
int intSomeInt = m_Dataset.MyTable.Rows[0].SomeInt;

then you will need to create a class that extends the DataSet class and a class called MyTable that extends the DataTable class, just the way that the VS.NET IDE does when you work with an XSD.

If this is absolutely necessary, I would recommend that do this using an XSD and let the VS.NET IDE create your code for you, then you can save the code generated by VS.NET and discard the .xsd file.
 
Hi Dalchri, thanks for your answer.

In my many attemtps, I did use the VS.NET IDE to create a class derived from Data.DataSet. It worked fine, and all the typing was right in the DataSet, my only problem with this solution is that it is not very flexible in the sense that everytime I add fields to my XML file I need to rebuild another class.

Two solutions come to my mind:

1) isn't it possible to just create an empty DataSet and create new DataColumn with the right type and then just load the XML in?


2) Another way I thought of this is the following:

I still do not want to provide a .XSD file to create the dataset, but, maybe I could create one at runtime?

Here's the idea:

a) Create a dummy dataset. Add all the columns that I know of, with the correct type.

b) Create a schema from this DataSet and then unreference it.

3) Create a new DataSet and create the inner tables with ReadXmlSchema, then load the Xml in.

Would that work?


Thanks again!

Huck
 
I think I see what you mean. You could create a class that only has a few of the fields in the data table that your code uses strong typed.

For this solution, I would still use a XSD. Perhaps you don't need the XSD to be flexible though. Could you only strong type the fields that your code actually uses?

For example:

Code:
if (MyTable[0].CustomerType = CustomerTypes.Retail) {
   //Code logic here
}

You use the CustomerID field here to make a logic decision in your code. Therefore, it needs to be strong typed.

But perhaps the CustomerName field is not touched by your code. You can then leave it out of the strongly typed XSD. This would create flexibility for adding additional data fields later. The only time you would need to rebuild would be if your code actually needs to interact with a new strongly typed field at which point you would have to add it to the XSD anyway.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top