Creating New Typed Datasets with the XML Designer
A typed dataset is a class with an associated XML Schema. If your application needs a typed dataset and there is no available data source to base your associated schema on, you can create a typed dataset schema manually using the XML Designer. If there is a data source that has the structure you want for your dataset, see Creating XML Schemas and Datasets from Existing Data.
To create a schema manually using the XML Designer
- In Solution Explorer, right-click the project you want to add the dataset to, select Add, and then select Add New Item.
- In the Add New Item dialog box, select Dataset, give the dataset a name, and then click Open.
Visual Studio adds a new, blank schema file (.xsd) to the project as well as a corresponding Dataset class file.Note The name you use will also be the name of the Dataset class file generated from the schema.
- To add a table definition to the schema, drag an element object from the XML Schema tab of the Toolbox onto the designer.
- Name the table by overwriting element1 in the box at the top of the element.
This will change the type of the element to match what typed in.
- To add a column definition to the table, move to a blank line in the table definition and select either element or attribute from the type drop-down.
- Assign a name and a data type to the new column definition.
Note When you create a column definition in a schema, you can create it as either an element or an attribute. The choice you make depends primarily on your own application requirements. If you are defining the schema to match a specification for sharing data with another application, then the specification will dictate your choice. If your dataset requires data types that are not built in to the W3C spec then you should define your columns as elements because elements are not restricted to the built in types, whereas attributes are.
- To add a primary key definition to the table, drag a key element from the XML Schema tab of the Toolbox onto the table definition,
- In the Key Editor dialog box:
- If the default name is not appropriate, type in a new name.
- In the Fields list, select the next empty line and use the drop-down list to choose the column to serve as the primary key.
- If there are multiple parts to the primary key, choose each part of the primary key separately, one per line.
- Check Primary Key.
The option to allow null keys is disabled.Tip To delete a key element, select the row with the key to be deleted, right-click it and choose Delete Key.
- Repeat Steps 3 through 8 for each table you want to add to the dataset.
- Save the schema.
The corresponding class file is regenerated to incorporate the changes made.
Creating XML Schemas and Datasets | Relationship Between XML Schemas and Datasets | Relational Data in XML Schemas | XML Files | XML Schemas and Data | Introduction to Data Access with ADO.NET | Introduction to Datasets