Introduction to XML Schema Creation in Visual Studio
Visual Studio 2005 has support for creating XML schemas. Launch Visual Studio 2005. Choose File from the New menu. The dialog shown in Figure 21-4 appears. Pick XML Schema from this dialog, and then click the Open button.
Figure 21-4. Creating a new XML schema file.
[View full size image]

Figure 21-5. Creating a new XML schema file.
[View full size image]

Listing 21-1. XML File Representing a Simple Order
The XML schema for this simple order XML file is created by following these steps:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns1:Order xmlns:ns1="http://tempuri.org/XMLSchema.xsd">
<ns1:CustomerName>Eric Carter</ns1:CustomerName>
</ns1:Order>
1. | Drag an element from the toolbox onto the schema design surface. |
2. | In the header row of the newly created element next to the E, type Order. |
3. | In the * row next to the asterisk (*), type CustomerName. |
Figure 21-6 shows the resulting designer view.
Figure 21-6. Design view of a simple Order schema.

Listing 21-2. XSD Schema File for a Simple Order Schema
Note that this schema is defined entirely with elements. An alternative way of representing this same data is by using an Order element and a CustomerName attribute. If CustomerName is defined as an attribute, the resultant XML is as shown in Listing 21-3.
<?xml version="1.0" encoding="utf-8"?>
<xs:schema targetNamespace="http://tempuri.org/XMLSchema.xsd"
elementFormDefault="qualified" xmlns="http://tempuri.org/XMLSchema.xsd"
xmlns:mstns="http://tempuri.org/XMLSchema.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Order">
<xs:complexType>
<xs:sequence>
<xs:element name="CustomerName" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Listing 21-3. XML File for a Simple Order That Uses an Attribute
The XML schema for an Order XML file that uses an attribute is created in Visual Studio by following these steps:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns1:Order xmlns:ns1="http://tempuri.org/XMLSchema.xsd"
CustomerName="Eric Carter">
</ns1:Order>
1. | Drag an element from the toolbox onto the schema design surface. |
2. | In the header row of the newly created element next to the E, type Order . |
3. | In the * row next to the asterisk (*), type CustomerName . |
4. | Click the E next to CustomerName. A drop-down will appear. Select attribute from the drop-down to convert CustomerName to an attribute. |
Figure 21-7 shows the resultant designer view.
Figure 21-7. Design view of a simple Order schema that uses an attribute.

Listing 21-4. XSD Schema File for a Simple Order Schema That Uses an Attribute
Excel works equally well with schemas that use attributes or elements. Word, however, does not work very well when you use attributes in a schema. If you are creating a schema that you need to use in Excel and Word, you should try to use elements instead of attributes. For more information, see Chapter 22, "Working with XML in Word."
<?xml version="1.0" encoding="utf-8"?>
<xs:schema targetNamespace="http://tempuri.org/XMLSchema.xsd"
elementFormDefault="qualified" xmlns="http://tempuri.org/XMLSchema.xsd"
xmlns:mstns="http://tempuri.org/XMLSchema.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Order">
<xs:complexType>
<xs:sequence />
<xs:attribute name="CustomerName" type="xs:string" />
</xs:complexType>
</xs:element>
</xs:schema>