- Introduction
- Downloading and Installing LINQ to XSD
- Generating Classes Against an XML Schema
- Programming with LINQ to XSD
- Using Regular Expressions to Add Restrictions to the Schema
- Summary
Generating Classes Against an XML Schema
Assuming that you have all of the beta bits, run Visual Studio and select New Project. In the Project Types list, select the LINQ to XSD Console Application template (see Figure 1). Project and item templates are a combination of wizard files, Jscript, and a generic wizard that begins with stubbed files containing replaceable elements. The wizard uses all of this information to create projects and project elements. The LINQ to XSD Console Application creates a console application with a reference to the Microsoft.Xml.Schema.Linq.DLL file.
Figure 1 The New Project dialog box, showing the location of the LINQ to XSD project templates.
To use LINQ to XSD, you'll need an XML file and an XML schema (.XSD) file that contains the schema definition for the XML file. You can use an existing pair of XML and XSD files, or create a simple XML file and infer the XSD file using code. For an example of inferring an XSD file, checkout my CodeGuru article "Inferring an XML Schema from an XML Document."
Listing 1 contains a partial list of customers from the Northwind database as XML, and Listing 2 contains an XML schema file that describes the schema for the Customers file. For the demo, the files were named Data.xml and Data.xsd.
Listing 1 An XML file containing some customers from the Northwind database.
<?xml version="1.0" encoding="utf-8" ?> <!--Generated XML--> <Root> <Customer> <CustomerID>ALFKI</CustomerID> <CompanyName>Alfreds Futterkiste</CompanyName> <ContactName>Paul Kimmel</ContactName> <ContactTitle>Sales Representative</ContactTitle> <Address>Obere Str. 57</Address> <City>Berlin</City> <Region></Region> <PostalCode>12209</PostalCode> <Country>Germany</Country> <Phone>030-0074321</Phone> <Fax>030-0076541</Fax> </Customer> <Customer> <CustomerID>ANATR</CustomerID> <CompanyName>Ana Trujillo Emparedados y helados</CompanyName> <ContactName>Ana Trujillo</ContactName> <ContactTitle>Owner</ContactTitle> <Address>Avda. de la Constitución 2222</Address> <City>México D.F.</City> <Region></Region> <PostalCode>05021</PostalCode> <Country>Mexico</Country> <Phone>(5) 555-4729</Phone> <Fax>(5) 555-3745</Fax> </Customer> </Root>
Listing 2 An XSD file that defines a schema matching the layout of the Customers file in Listing 1.
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="Root"> <xs:complexType> <xs:sequence> <xs:element maxOccurs="unbounded" name="Customer"> <xs:complexType> <xs:sequence minOccurs="0"> <xs:element name="CustomerID" type="xs:string" /> <xs:element name="CompanyName" type="xs:string" /> <xs:element name="ContactName" type="xs:string" /> <xs:element name="ContactTitle" type="xs:string" /> <xs:element name="Address" type="xs:string" /> <xs:element name="City" type="xs:string" /> <xs:element name="Region" type="xs:string" /> <xs:element name="PostalCode" type="xs:string" /> <xs:element name="Country" type="xs:string" /> <xs:element name="Phone" type="xs:string" /> <xs:element name="Fax" type="xs:string" /> </xs:sequence> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
You need to change the build action for the XSD file to use the LinqToXsd.exe compiler, in order to write the strongly typed classes for the schema. To set the build action, follow these steps:
- In the Solution Explorer, click the data.xsd file.
- Press F4 to open the properties window for the selected file.
- In the Build Action property, set the build action to LinqToXsdSchema (see Figure 2).
- Build the project.
Figure 2 Changing the build action to LinqToXsdSchema causes code generation to occur when you build the project.
Remember, you haven't written any code yet. All you're doing to this point is causing code generation to occur. The generated code will create classes that map to the schema in the .XSD file. You can view the generated elements in the Object Browser (View > Object Browser), as shown in Figure 3. The code generated types are compiled into the executable, which you can verify with a decompiler such as Reflector.
Figure 3 After you build the project, strongly typed elements will be available for reference; you can view these elements in the Object Browser.