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.

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. 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. For example, the simple XML file shown in Listing 21-1 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



 Eric Carter

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 in the Save As dialog. 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 elementsOrder and CustomerName. 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 misleadingthe way the XSD file is defined it will be a sequence of one and only one CustomerName elements. 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. For example, by setting minOccurs to 1 and maxOccurs to unbounded, 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



 
 
 
 
 
 
 

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.

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:

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 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 sequencethis sequence can actually be removed without affecting the schema. It then defines CustomerName as an attribute of type string.

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


Part One. An Introduction to VSTO

An Introduction to Office Programming

Introduction to Office Solutions

Part Two. Office Programming in .NET

Programming Excel

Working with Excel Events

Working with Excel Objects

Programming Word

Working with Word Events

Working with Word Objects

Programming Outlook

Working with Outlook Events

Working with Outlook Objects

Introduction to InfoPath

Part Three. Office Programming in VSTO

The VSTO Programming Model

Using Windows Forms in VSTO

Working with Actions Pane

Working with Smart Tags in VSTO

VSTO Data Programming

Server Data Scenarios

.NET Code Security

Deployment

Part Four. Advanced Office Programming

Working with XML in Excel

Working with XML in Word

Developing COM Add-Ins for Word and Excel

Creating Outlook Add-Ins with VSTO



Visual Studio Tools for Office(c) Using C# with Excel, Word, Outlook, and InfoPath
Visual Studio Tools for Office(c) Using C# with Excel, Word, Outlook, and InfoPath
ISBN: 321334884
EAN: N/A
Year: N/A
Pages: 214

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