views:

77

answers:

2

I use a DataGrid to show a xml file. The Grid's DataSource is a Typed DataSet.(using schema)

    Assembly assembly = Assembly.GetExecutingAssembly();
    Stream stream = assembly.GetManifestResourceStream("XML_Reader.Resources.schema.xsd");
    XmlSchemaSet schemas = new XmlSchemaSet();
    XmlReaderSettings settings = new XmlReaderSettings();
    settings.ValidationType = ValidationType.Schema;
    settings.Schemas.Add(null, XmlReader.Create(stream));
    using (XmlReader reader = XmlReader.Create(xmlFile, settings))
    {
        newDataSet.ReadXml(reader);
    }
    dataGrid.DataSource = newDataSet;


I added a xsd schema to my project and used MSDataSetGenerator to generate the newDataSet. (VS2008).
Now i want to create a new DataSet object for every new (hierarchical xml) file i read.

Creating a new DataSet object isn't a problem but the data types aren 't correct, so i can't sort them well (specifically the numerical fields). In my view, i need to create a new Typed DataSet.

So how can i fix this ?

A: 

Let me answer my own question ;-))

A typed DataSet is simply a class you can instantiate like any other class.There is no magic to anything generated by tools, those tools simply generate classes and you can use those classes the same way you use other classes.

Do NewDataSet d1 = new NewDataSet(); where you put the right class name there instead of "NewDataSet".

Robertico
A: 

Datasets in Visual Studio Overview -

Typed Versus Untyped Datasets

A typed dataset is a dataset that is first derived from the base DataSet class and then uses information from the Dataset Designer, which is stored in an .xsd file, to generate a new strongly-typed dataset class. Information from the schema (tables, columns, and so on) is generated and compiled into this new dataset class as a set of first-class objects and properties. Because a typed dataset inherits from the base DataSet class, the typed class assumes all of the functionality of the DataSet class and can be used with methods that take an instance of a DataSet class as a parameter.
An untyped dataset, in contrast, has no corresponding built-in schema. As in a typed dataset, an untyped dataset contains tables, columns, and so on — but those are exposed only as collections. (However, after manually creating the tables and other data elements in an untyped dataset, you can export the dataset's structure as a schema using the dataset's WriteXmlSchema method.)

Yes you can automatically generate these with Visual Studio:

Or, you can create your own strongly typed DataSets (this is cleaner IMO). Example:

using System.Data;

public class CatsDataTable : DataTable
{
  public CatsDataTable() : base()
  {
    base.TableName = "cats";
    Columns.Add(new DataColumn(SqlTokens.id_cats, typeof(int)));
    Columns.Add(new DataColumn(SqlTokens.owners_id_owners, typeof(int)));
    Columns.Add(new DataColumn(SqlTokens.cats_name, typeof(string)));
    Columns.Add(new DataColumn(SqlTokens.cats_number_of_spots, typeof(int)));
  }
}

public class OwnersDataTable : DataTable
{
  public OwnersDataTable() : base()
  {
    base.TableName = "owners";
    Columns.Add(new DataColumn(SqlTokens.id_owners, typeof(int)));
    Columns.Add(new DataColumn(SqlTokens.owners_name, typeof(string)));
  }
}

public class PetsDataSet : DataSet
{
  public PetsDataSet() : base()
  {
    base.TableName = "pets";
    Tables.Add("cats");
    Tables.Add("owners");
  }
}

Tangent about Garbage Collection

garbage collection (GC) is a form of automatic memory management. It is a special case of resource management, in which the limited resource being managed is memory. The garbage collector, or just collector, attempts to reclaim garbage, or memory occupied by objects that are no longer in use by the program. (wikipedia)
JohnB
The page (***How to: Create a Typed Dataset***) you requested could not be found. But after you automatically generate one with Visual Studio you can use that class creating a new DataSet for every file. The old one will be garbage collected by .NET.
Robertico
@Robertico: thanks for pointing out the typo in my post (+1), I fixed it. However, creating class definitions (whether using a code gen tool or not) and Garbage Collection are two different concepts, and I'm not sure that you understand the difference. I added a couple links in my answer.
JohnB
@JohnB: I understand the concepts and used the Garbage Collection also.
Robertico
@Robertico: what do you mean by "and used the Garbage Collection"? The Garbage Collector is automatic.
JohnB
@JohnB: I use `GC.Collect();` to force a garbage collection request. If, when, and how it runs is up to the Common Language Runtime (CLR).To request that the CLR deallocate unreferenced object. It reduces my memory usage.
Robertico
@Robertico: but it might also reduce your performance, the GC is supposed to be smart about when to dealloc. But good point, I'm glad I asked you what you meant.
JohnB