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 21.4 appears. Pick XML Schema from this dialog box and then click the Open button.

Figure 21.4. Creating a new XML schema file.


Visual Studio shows a design view for creating XML schema, as shown in Figure 21.5. The toolbox has XML schema objects that can be dragged onto the design surface for the new XML schema.

Figure 21.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.

Listing 21.1. XML File Representing a Simple Order

<?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> 


The XML schema for this simple Order XML file is created by following these steps:

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.


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.

Listing 21.2. XSD Schema File for a Simple Order Schema

<?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> 


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.

Listing 21.3. XML File for a Simple Order That Uses an Attribute

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <ns1:Order xmlns:ns1="http://tempuri.org/XMLSchema.xsd" CustomerName="Eric Carter"> </ns1:Order> 


The XML schema for an Order XML file that uses an attribute is created in Visual Studio by following these steps:

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 list will appear.

5.

Select an attribute from the drop-down list 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 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.

Listing 21.4. XSD Schema File for a Simple Order Schema That Uses an Attribute

<?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> 


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."




Visual Studio Tools for Office(c) Using Visual Basic 2005 with Excel, Word, Outlook, and InfoPath
Visual Studio Tools for Office: Using Visual Basic 2005 with Excel, Word, Outlook, and InfoPath
ISBN: 0321411757
EAN: 2147483647
Year: N/A
Pages: 221

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net