Listing 1: An XSD schema for products

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.microsoft.com/schemas/northwind/products"
xmlns:prod="http://www.microsoft.com/schemas/northwind/products">
	<xs:element name="Product">
		<xs:complexType>
			<xs:sequence>
				<xs:element ref="prod:ProductID" />
				<xs:element ref="prod:ProductName" />
				<xs:element ref="prod:SupplierID" />
				<xs:element ref="prod:CategoryID"  />
				<xs:element ref="prod:QuantityPerUnit" />
				<xs:element ref="prod:UnitPrice" />
				<xs:element ref="prod:UnitsInStock" />
				<xs:element ref="prod:UnitsOnOrder" />
				<xs:element ref="prod:ReorderLevel" />
				<xs:element ref="prod:Discontinued" />
			</xs:sequence>
		</xs:complexType>
	</xs:element>

    <xs:element name="ProductID" type="xs:integer" />
    <xs:element name="ProductName" type="xs:string" />
    <xs:element name="SupplierID" type="xs:integer" />
    <xs:element name="CategoryID" type="xs:integer" />
    <xs:element name="QuantityPerUnit" type="xs:string" />
    <xs:element name="UnitPrice" type="xs:double" />
    <xs:element name="UnitsInStock" type="xs:integer" />
    <xs:element name="UnitsOnOrder" type="xs:integer" />
    <xs:element name="ReorderLevel" type="xs:integer" />
    <xs:element name="Discontinued" type="xs:boolean" />

</xs:schema>

Listing 2: Creating a SQL table based on an XSD

CREATE TABLE ProductDocs (
	ID INT IDENTITY PRIMARY KEY,
	ProductDoc XML('http://www.microsoft.com/schemas/northwind/products')
		NOT NULL)

Listing 3: Inserting into the products table

INSERT INTO ProductDocs VALUES('
    <Product xmlns="http://www.microsoft.com/schemas/northwind/products">
        <ProductID>1</ProductID>
        <ProductName>Chai</ProductName>
        <SupplierID>1</SupplierID>
        <CategoryID>1</CategoryID>
        <QuantityPerUnit>10 boxes x 20 bags</QuantityPerUnit>
        <UnitPrice>18.0000</UnitPrice>
        <UnitsInStock>39</UnitsInStock>
        <UnitsOnOrder>0</UnitsOnOrder>
        <ReorderLevel>10</ReorderLevel>
        <Discontinued>0</Discontinued>
    </Product>
')

Listing 4: Loading an XPathDocument

XPathDocument doc = new XPathDocument("products.xml");
XPathEditableNavigator editor = doc.CreateEditor();
// move to the description node
editor.MoveToFirstChild();
// move to the next element
editor.MoveToNext();
// create an XmlWriter that appends a child node to the current //position
using (XmlWriter writer = editor.AppendChild())
{
   writer.WriteStartElement("product");
   writer.WriteAttributeString("ItemNumber", "1234567");
   writer.WriteAttributeString("OrderDate", "9-27-2004");
   writer.WriteAttributeString("VendorID", "12345");
   writer.WriteElementString("Description", "Red Jacket with trim");
   writer.WriteEndElement();
}
doc.Save("products.xml");

Listing 5: Validating against an XSD schema

XmlSchemaSet schemaSet = new XmlSchemaSet();
schemaSet.Add("http://myproducts.examples.com", "products.xsd");
schemaSet.Compile();
XPathDocument doc = new XPathDocument("products.xml");
XPathEditableNavigator editor = doc.CreateEditor();
editor.Validate(schemaSet, new ValidationEventHandler(ValidationCallback));

public static void ValidationCallback(object sender, ValidationEventArgs args)
{

   if(args.Severity == XmlSeverityType.Warning)
      Console.Write("Warning: ");
   else if(args.Severity == XmlSeverityType.Error)
      Console.Write("Error: ");
   Console.WriteLine(args.Message);.