Re: Convert an untyped Data Table to a typed Data Table

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



John Wright wrote:
I need to convert some of my untyped datatables to typed datatables. Is
there a way to do this? I am assuming I need to convert the table to XML
then back, but I am unsure. Does anyone have any code or can point me in the
right direciton? I really appreciate the help.

The easier and most flexible solution (IMHO) is the dataset designer
which will generate a dataset with datatables built after the schemma
of an existing database. It will create the dataset, the datatables,
the relations between the tables and even the table adapters.

If you have the datatables but no database, you may use the xsd.exe
utility (in my setup it lives in %programfiles%\Microsoft Visual
Studio 8\SDK\v2.0\Bin"). This utility will practically mimic the work
of the designer (except for not automagically creating the table
adapters). It feeds on XSD files and generates datasets (or classes)
based on that. The nice touch is that it can create a XSD file based
on a sample XML file.

For example, given the following Dept.xml file:

<Dept>
<Employee>
<Id>10</Id>
<Name>Jack</Name>
</Employee>
<JobType>
<Id>1</Id>
<Name>Director</Name>
</JobType>
</Dept>

after executing "xsd Dept.xml", a Dept.xsd file is created (beware
word wrap):

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="Dept" xmlns="" xmlns:xs="http://www.w3.org/2001/
XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="Dept" msdata:IsDataSet="true"; msdata:Locale="en-
US">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="Employee">
<xs:complexType>
<xs:sequence>
<xs:element name="Id" type="xs:string" minOccurs="0" />
<xs:element name="Name" type="xs:string" minOccurs="0" /

</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="JobType">
<xs:complexType>
<xs:sequence>
<xs:element name="Id" type="xs:string" minOccurs="0" />
<xs:element name="Name" type="xs:string" minOccurs="0" /

</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>

You may edit the XSD to your liking (for example, changing the type
attribute of both Id columns from "xs:string" to xs:int"). Afterwards,
executing "xsd Dept.xsd /dataset /language:VB" generates the Dept.vb
file, containing the declaration for a strongly typed "Dept" dataset
as well as the two strongly typed tables "Employee" and "JobType".
Maybe the resulting code isn't pretty enough to your tastes (it isn't
to mine), but it makes a stepping stone for your further edits.

HTH.

Regards,

Branco.
.



Relevant Pages

  • Re: Convert an untyped Data Table to a typed Data Table
    ... write the LINQ over ADO.NET with untyped datatables, but it is nice to have ... It feeds on XSD files and generates datasets ... You may edit the XSD to your liking (for example, ...
    (microsoft.public.dotnet.languages.vb)
  • Re: 3-D String Array (or better alternative?)
    ... saving device that switches from using an internal array and linear look-up to ... A DataTable - DataColumnCollection and DataRowCollection ... The unnecessary size I'm thinking of is the> XML involved in storing this as a DataTable (I haven't measured the bytes> involved, but we all know XML is verbose - more so than a 3d array or> Hashtable - and DataTables are XML structures.). ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: SqlDataReader question
    ... That said, the DataReader has the least amount of overhead, and is faster. ... DataSets and DataTables are disconnected Record ... The DataSet is also implicitly serializable as XML. ... > DataSets the preferred solution then? ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: SqlDataReader question
    ... That said, the DataReader has the least amount of overhead, and is faster. ... DataSets and DataTables are disconnected Record ... The DataSet is also implicitly serializable as XML. ... > DataSets the preferred solution then? ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: NewRow fails when using XML document as Dataset
    ... the XML fragment, ... there are 2 DataTables in the above XML ... cons'up an XmlTextReader to load the file (then close the ...
    (microsoft.public.dotnet.xml)