2.18. Controlling the Names Used in a Strongly Typed DataSet

Problem

You want to assign your own names to the classes and properties for strongly typed DataSet classes.

Solution

Use annotations in the XML schema to control the names of classes and properties in strongly typed DataSet classes.

The sample uses one XSD file:

CategoriesDS_AnnotatedName.xsd

The schema used to generate the strongly typed DataSet. The schema is annotated so that you can access the collection of rows in the table by using the Categorys property of the DataSet rather than categories, each row by using the Category property of the row collection rather than CategoriesRow, and the CategoryName field by using the Name property of the row rather than CategoryName. The annotations are marked in bold in the Example 2-23.

Example 2-23. File: TypedDataSets\CategoriesDS_AnnotatedName.xsd

<?xml version="1.0" standalone="yes" ?>
<xs:schema id="CategoriesDS_AnnotatedName"
    targetNamespace=
    "http://www.tempuri.org/CategoriesDS_AnnotatedName.xsd"
    xmlns:mstns="http://www.tempuri.org/CategoriesDS_AnnotatedName.xsd"
    xmlns="http://www.tempuri.org/CategoriesDS_AnnotatedName.xsd"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"
    xmlns:codegen="urn:schemas-microsoft-com:xml-msprop" attributeFormDefault="qualified" elementFormDefault="qualified"> <xs:element name="CategoriesDS_AnnotatedName" msdata:IsDataSet="true"> <xs:complexType> <xs:choice maxOccurs="unbounded"> <xs:element name="Categories" ...

Get ADO.NET Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.