This section deals with a more complex end-to-end scenario that puts together the schema creation capabilities of Visual Studio and the schema mapping capabilities of Excel. When you take a schema and map it into Excel using the XML Source task pane, you enable the exporting and importing of XML data in the spreadsheet. We are going to create an Excel spreadsheet that can be used to record a customer's book order. The spreadsheet will support the import and export of XML that conforms to our book order schema. The spreadsheet will look like Figure 21-8.
Figure 21-8. An Excel spreadsheet for processing a book order.
Listing 21-5 shows the XML that this spreadsheet will be able to import and export.
Listing 21-5. XML File Generated from Book Order Spreadsheet
Eric Carter 2005-02-19 Windows Forms Programming in C# 0-321-11620-8 Addison-Wesley 49.99 Effective C# 0-321-24566-0 Addison-Wesley 39.99 The C# Programming Language 0-321-15491-6 Addison-Wesley 29.99 119.97 10.7973 130.7673
Creating the Schema Using Visual Studio
To create this schema using Visual Studio, follow these steps:
1. |
Start Visual Studio 2005. |
2. |
Create a new XSD file by choosing File from the New menu of the File menu or by pressing Ctrl+N. |
3. |
Choose XML Schema from the list of Visual Studio installed templates, as shown in Figure 21-4. Then click the Open button. |
4. |
The Schema design view appears as shown in Figure 21-5. Drag an element object off of the toolbox onto the design surface. |
5. |
Type Order and press the Enter key. |
6. |
In the * row, type CustomerName and press the Enter key. |
7. |
In the * row, type Date and press the Tab key, and then type date for the data type and press Enter. |
8. |
In the * row, type Subtotal and press the Tab key, and then type float for the data type and press Enter. |
9. |
In the * row, type Tax and press the Tab key, and then type float for the data type and press Enter. |
10. |
In the * row, type Total and press the Tab key, and then type float for the data type and press Enter. |
11. |
Now right-click the Order element box and choose New element from the Add menu. |
12. |
Type Book and press the Enter key. |
13. |
In the * row of the newly created Book element, type Title and press Enter. |
14. |
In the * row of the newly created Book element, type ISBN and press Enter. |
15. |
In the * row of the newly created Book element, type Publisher and press Enter. |
16. |
In the * row of the newly created Book element, type Price and press the Tab key, and then type float for the data type and press Enter. |
17. |
We now want to specify that multiple books can be included in an order. Click the Book row in the Order element box and show the Properties window by choosing Properties Window from the View menu. For the property maxOccurs, type unbounded. For the property minOccurs, type 1. |
18. |
Now save the schema using the Save As command from the File menu. In the Save File As dialog, drop down the Save as type combo box and pick XML Schema Files (*.xsd). For the filename, type BookOrder.xsd and save it to a convenient place such as the desktop. |
Figure 21-9 show what the final schema in Visual Studio should look like.
Figure 21-9. The book order schema in Visual Studio.
Listing 21-6 shows the generated XSD file. Note that the sequence of Book elements in an Order element is now a sequence with a minimum (minOccurs) of one Book elements and a maximum (maxOccurs) of unbounded Book elements. This will allow our schema to represent one or more Books in an Order. Also, having a sequence where maxOccurs is greater than one or unbounded will help Excel to know that it needs to represent the Books in an Order using an Excel list.
Listing 21-6. Book Order Schema XSD File
Adding a Schema to the Excel Workbook
Now that we have created a schema, let's add it to an Excel workbook. Launch Excel and create a new empty workbook. Bring up the Excel XML Source task pane as described in the first section of this chapter. You should now see the XML Source task pane with no mappings as yet in the task pane. To add an XML map, click the XML Maps button in the XML Source task pane. Doing so brings up the dialog shown in Figure 21-10.
Figure 21-10. The XML Maps dialog.
Click the Add button and browse to wherever you saved your book order schema. Select the schema and click the Open button. The XML map now appears as a loaded XML map in the workbook. Using this dialog, you can delete and rename an XML map. For now, we will just click the OK button to exit this dialog.
The Excel XML Source task pane shows the XML map we just added, as shown in Figure 21-11.
Figure 21-11. The XML Source task pane with an XML map.
Mapping the Schema to the Excel Workbook
The XML Source task pane represents our book order schema in a tree view. The icon associated with Order indicates a required parent element. The icons associated with CustomerName, Date, Title, ISBN, Publisher, Price, Subtotal, Tax, and Total indicate required child elements. The icon associated with Book indicates a required repeating parent element. Excel also supports other schema constructs such as attributes and nonrequired elements and attributes. These constructs also have their own icons.
Let's try a few different ways of mapping the schema into the workbook. The first approach we will take is to click the root ns1:Order node in the XML Source task pane and drag it to cell A1 in the workbook. Excel creates one list to contain all the data, as shown in Figure 21-12.
Figure 21-12. The list created when ns1:Order is dragged to cell A1.
The XML Source task pane now indicates that all the elements have been mapped by bolding each element that has been mapped, as shown in Figure 21-13. Parent elements such as Order and Book are not mapped explicitly in the Workbook because these containing relationships do not need to be directly mapped to an Excel cell or list. You can remove a mapping by selecting the mapped cell or list in the Workbook and pressing the Delete key. You can also right-click the elements in the XML Source task pane that are in bold and choose Remove Element to remove the mapping.
Figure 21-13. Mapped elements are bolded in the XML Source task pane.
At the very bottom of the XML Source task pane is a link that says Verify Map for Export. Click this link. Excel displays the dialog shown in Figure 21-14.
Figure 21-14. Mapping cannot be exported because of denormalized data.
To consider why this mapping cannot be exported, let's import the XML in Listing 21-5 into our current mapping. From the XML menu of Excel's Data menu, choose Import. Browse to an XML file containing the XML in Listing 21-5 and click the Import button. Because of the mapping we have established, Excel knows how to bring the XML into the list defined in the worksheet. Figure 21-15 shows the resulting worksheet.
Figure 21-15. Result of importing the XML in Listing 21-5.
The error we got when clicking Verify Map for Export was that the mapping contained denormalized data. In Figure 21-15, we have highlighted the data that is denormalized and redundant. If we were to try to export this list by selecting Export from the XML menu of Excel's Data menu, Excel would fail to export because it would not know how to deal with the redundant data.
Let's clear out this mapping and try to create a mapping that can be successfully exported as XML. Select the whole worksheet by pressing Ctrl+A, and then press the Delete button. This time, we are going to drag in CustomerName, Date, Subtotal, Tax, and Total as individual cell mappings, and we will map the Book element sequence as a list.
To prepare the spreadsheet for mapping, let's put in some labels in advance. Figure 21-16 shows the resulting spreadsheet.
Figure 21-16. Preparing the spreadsheet for mapping.
Now, do the following to map the nonrepeating elements to cells in the spreadsheet:
1. |
Drag the CustomerName element from the XML Source task pane to cell C3. |
2. |
Drag the Date element from the XML Source task pane to cell C4. You will be prompted that the formatting in the cell does not match the format of the data. This is Excel noticing that the format of the cell you are mapping to is not formatted to contain a date. Click the Match element data type button to continue and format the mapped cell as a date. |
3. |
Drag the Subtotal element from the XML Source task pane to cell C9. |
4. |
Drag the Tax element from the XML Source task pane to cell C10. |
5. |
Drag the Total element from the XML Source task pane to cell C11. |
Finally, let's map the repeating elements to a list:
We are also going to use some features of Excel in our spreadsheet:
The spreadsheet will now look like the one shown in Figure 21-17. Note the blue borders around all the mapped cells or lists. You can hide these blue borders by using the Options button in the XML Source task pane. Click the Options button, and then check the option from the pop-up menu that says Hide Border of Inactive Lists.
Figure 21-17. The final mapped spreadsheet.
Now, fill out the spreadsheet to make it look like Figure 21-8. Note that when you have the list selected, a new row marked with * displays, in which you can enter new items. Also note that as you type prices, the totals row sums up the prices in the list and the formulas in the spreadsheet calculate the Tax and Total.
Now, with the spreadsheet filled out, let's export the data in the spreadsheet as XML conforming to the schema we have mapped. We have assumed that this mapping will be exportable. To verify that, click the Verify Map for Export link in the XML Source task pane. A dialog should appear that says that our mapping is exportable.
From the XML menu of the Data menu, choose Export. Type the name of the XML file you want to export to, something like bookorder.xml. Then, after exporting the file, go open it in a text editor such as Notepad. You should see the XML very similar to that shown in Listing 21-5.
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