Jan. 17, 2011, 11:22 p.m.
posted by ska
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 box shown in Figure.4 appears. Pick XML Schema from this dialog box and then click the Open button.
4. Creating a new XML schema file.
Visual Studio shows a design view for creating XML schema, as shown in Figure.5. The toolbox has XML schema objects that can be dragged onto the design surface for the new XML schema.
5. Design view, creating a new XML schema file.
The schema object we will use most frequently in this example is element. An XML schema element defines an element in an XML file. The simple XML file shown in Listing 21.1, for example, has an element called Order in the namespace ns1. It also has an element called CustomerName in the namespace ns1. The CustomerName element is parented by the Order element.
XML File Representing a Simple Order
The XML schema for this simple Order XML file is created by following these steps:
Figure.6 shows the resulting designer view.
6. Design view of a simple Order schema.
When you save this schema, use the Save As command from the File menu to save it as order.xsd. You will have to pick XML Schema Files (*.xsd) from the Save As Type drop-down list in the Save As dialog box. The order.xsd file will look like the one shown in Listing 21.2. You can see that an XML schema is just another XML file that defines what constitutes a valid Order XML file. It defines two elements: Order and Customer Name. Because the Order element contains other elements, it is defined as a complexType. It contains a sequence of CustomerName elements that are of type string. Sequence in this case is misleading; the way the XSD file is defined it will be a sequence of one, and only one, CustomerName element. It is possible to define a sequence that has a varying number of elements in it using the maxOccurs and minOccurs settings, which we consider later in this chapter. By setting minOccurs to 1 and maxOccurs to unbounded, for example, you could allow one or more CustomerName elements to be associated with an order.
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 to use 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 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:
Figure.7 shows the resultant designer view.
7. Design view of a simple Order schema that uses an attribute.
Listing 21.4 shows the schema for an order using an attribute. Because the Order element contains other attributes, it is defined as a complexType. It contains an empty sequence; this sequence can actually be removed without affecting the schema. Then it defines CustomerName as an attribute of type string.
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."