Professional ASP.NET 2.0 XML
01_596772 ffirs.qxd 12/13/05 11:22 PM Page i
01_596772 ffirs.qxd 12/13/05 11:22 PM Page ii
Professional ASP.NET 2.0 XML
Thiru Thangarathinam
01_596772 ffirs.qxd 12/13/05 11:22 PM Page iii
Professional ASP.NET 2.0 XML
Published by
Wiley Publishing, Inc.
10475 Crosspoint Boulevard
Indianapolis, IN 46256
www.wiley.com
Copyright © 2006 by Wiley Publishing, Inc., Indianapolis, Indiana
Published simultaneously in Canada
ISBN-13: 978-0-7645-9677-3
ISBN-10: 0-7645-9677-2
Manufactured in the United States of America
10 9 8 7 6 5 4 3 2 1
No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form
or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as per-
mitted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior writ-
ten permission of the Publisher, or authorization through payment of the appropriate per-copy fee to
the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978)
646-8600. Requests to the Publisher for permission should be addressed to the Legal Department, Wiley
Publishing, Inc., 10475 Crosspoint Blvd., Indianapolis, IN 46256, (317) 572-3447, fax (317) 572-4355, or
online at
/>LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: THE PUBLISHER AND THE AUTHOR
MAKE NO REPRESENTATIONS OR WARRANTIES WITH RESPECT TO THE ACCURACY OR
COMPLETENESS OF THE CONTENTS OF THIS WORK AND SPECIFICALLY DISCLAIM ALL WAR-
RANTIES, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR
number of books for Wrox Press in .NET technologies. Thiru is also a regular contributor to print and
online magazines such as Visual Studio Magazine, Visual Studio .NET Professional, SQL Server
Professional, DevX, ASPToday.com, 15seconds.com, and Developer.com. At Intel, he is part of the team
that is focused on developing the Enterprise Architecture and Service Oriented Architectures for Intel.
He can be reached at
01_596772 ffirs.qxd 12/13/05 11:22 PM Page v
01_596772 ffirs.qxd 12/13/05 11:22 PM Page vi
Credits
Senior Acquisitions Editor
Jim Minatel
Development Editor
Ed Connor
Technical Editor
Kirk Evans
Production Editor
Pam Hanley
Copy Editor
Susan Hobbs
Editorial Manager
Mary Beth Wakefield
Production Manager
Tim Tate
Vice President and Executive Group Publisher
Richard Swadley
Vice President and Executive Publisher
Joseph B. Wikert
Project Coordinator
Ryan Steffen
Graphics and Production Specialists
The XML Advantage 20
Summary 21
Chapter 2: Introduction to ASP.NET 2.0 23
ASP.NET 2.0 Features 23
Developer Productivity 23
Administration and Management 35
Speed and Performance 37
Summary 40
Chapter 3: XML Classes in the .NET Framework 41
XML Support in the .NET Framework 2.0 41
Design Goals for XML Support in .NET Framework 2.0 41
XML Namespaces 42
XML Parsing 43
02_596772 ftoc.qxd 12/13/05 11:12 PM Page ix
x
Contents
Writing XML 46
XPath Support 46
XML Schema Object Model (SOM) 47
Understanding XML Validation 49
Transforming XML Data using XSLT 49
XML Serialization 51
XML Web Services 52
XML and ADO.NET 56
ASP .NET Configuration 57
Summary 59
Chapter 4: Reading and Writing XML Data Using XmlReader and XmlWriter 61
XML Readers and Writers 62
Reading XML with XmlReader 63
Overview of XmlReader 63
Validating XML in an XmlDocument 171
Summary 171
Chapter 7: Transforming XML Data with XSLT 173
A Primer on XSLT 174
What Is XSLT, XSL, and XPath? 174
Need for XSLT 175
XSLT Elements 176
XSLT Functions 179
Applying an XSL Style Sheet to an XML Document 179
.NET Classes Involved in XSL Transformation 186
User Defined Functions in an XSL Style Sheet 193
The XsltSettings Class 198
A Complete Example 199
Advanced XSLT Operations 207
Debugging XSLT Style Sheets 209
Summary 211
Chapter 8: XML and ADO.NET 213
ADO.NET and XML 214
Loading XML into a DataSet 214
DataSet Schemas 218
Transforming DataSet to XML 222
Typed DataSets 230
XmlDataDocument Object and DataSet 235
Relationship between XmlDataDocument
and XPathNavigator 242
DataTable and XML 243
Summary 245
Chapter 9: XML Data Display 247
ASP.NET 2.0 Hierarchical Data Controls 248
Site Navigation 248
Implementation of Web Site 349
Putting It All Together 374
Summary 375
Chapter 12: XML Serialization 377
A Primer on Serialization 378
The XmlSerializer Class 379
Advanced Serialization 384
Deserializing XML 394
Generics and XML Serialization 403
Pregenerating Serialization Assemblies 407
Handling Exceptions 408
Summary 409
02_596772 ftoc.qxd 12/13/05 11:12 PM Page xii
xiii
Contents
Chapter 13: XML Web Services 411
XML Web Service 412
Building an ASP.NET Web Service 412
Creating a Proxy Class for the Web Service 416
Returning Complex Types 420
Using SOAP Headers 431
Using SOAP Extensions 436
Asynchronous Invocation of Web Services from a Client Application 443
Asynchronous Invocation of Web Services from a Browser Using IE Web Service Behavior 448
Asynchronous Web Service Methods 454
Controlling XML Serialization Using IXmlSerializable 457
Using Schema Importer Extensions 460
Miscellaneous Web Service Features in .NET
Framework 2.0 463
Summary 464
Summary 539
Index 541
02_596772 ftoc.qxd 12/13/05 11:12 PM Page xiv
Acknowledgments
I would like to acknowledge my wife Thamiya, my parents and my family for their constant support
and encouragement throughout while I spent nights and weekends working on this book.
03_596772 flast.qxd 12/13/05 11:17 PM Page xv
03_596772 flast.qxd 12/13/05 11:17 PM Page xvi
Introduction
This book will cover the intersection between two great technologies: ASP.NET and XML.
XML has been a hot topic for some time. The massive industry acceptance of this W3C Recommendation,
which allows data communication and information storage in a platform independent manner, has been
astounding. XML is seen and used everywhere—from the display of data on various browsers using the
transformation language XSLT, to the transport of messages between Web services using SOAP.
.NET is Microsoft’s evolutionary and much vaunted new vision. It allows programming of applications
in a language independent manner, the sharing of code between languages, self-describing classes, and
self-documenting program code to name but a few of its capabilities. .NET, in particular ASP.NET, has
been specifically designed with Web services and ease of development in mind. With the release of .NET
2.0 Framework, .NET includes significant enhancements to all areas of ASP.NET. For Web page develop-
ment, new XML data controls like XmlDataSource, and TreeView make it possible to display and edit
data on an ASP.NET Web page without writing code reducing the required amount of code by as much
as 70% in some cases. ADO.NET 2.0 includes many new features that allow you to leverage the new
XML features introduced with SQL Server 2005 (the next major release of SQL Server).
To achieve this exciting new Web programming environment, Microsoft has made extensive use of XML.
In fact, no other technology is so tightly bound with ASP.NET as XML. It is used as the universal data
format for everything from configuration files to metadata, Web Services communication, and object
serialization. All the XML capabilities in the System.Xml namespace were significantly enhanced for
added performance and standards support. The new model for processing in-memory XML data,
editable XPathNavigator, new XSLT processor, strong typed support for XmlReader, and XmlWriter
classes, are some of the key XML related improvements. Connected to this is the new support for XML
An Introduction to XML
XML finds several applications in business and, increasingly, in everyday life. It provides a common
data format for companies that want to exchange documents using Web services. This chapter is about
XML as a language and its related technologies. The XML technologies that I will specifically introduce
in this chapter are: XML document elements, namespaces, entities, DTD, XDR, XSD, XSD schema data
types, XSLT, XML DOM, XPath, SAX, XLink, XPointer, and XQuery.
An Introduction to ASP.NET 2.0
In Chapter 2, I aim to give the reader an overview of the new features of ASP.NET 2.0. I will highlight
the new ASP.NET page architecture, new data controls, and code sharing features. I ask, “What is master
pages” and go on to talk about how master pages and themes aid in creating consistent Web sites. Later
on, I look at security controls and Web parts framework and illustrate how ASP.NET 2.0 enables 70%
code reduction. Finally, I will look at the new caching and administration and management functionali-
ties of ASP.NET 2.0.
XML Classes in the .NET Framework
In Chapter 3, I take a brisk walk through all the new XML classes in the .NET Framework, which will be
discussed in more detail throughout the rest of the book.
Microsoft has introduced several new applications of XML in .NET 2.0 and has also done some innova-
tive work to improve the core XML API. I start with a discussion on the use of XML in configuration
files, DOM, XSD schema validation, XSLT transformations, XML serialization, Web services, and XML
Introduction
xviii
03_596772 flast.qxd 12/13/05 11:17 PM Page xviii
support in ADO.NET and look at the namespaces and classes that are available for this purpose. I will
also illustrate the new ASP.NET configuration enhancements and take a quick look at the configuration
classes in .NET Framework 2.0.
Reading and Writing XML
Chapter 4 starts a section of chapters (4 through 6) that look at the functionality contained within the
System.Xml in more detail.
In particular, here I look at the fast, forward-only read-only mechanisms provided by the .NET
Framework for reading and writing XML documents, namely the XmlReader and XmlWriter classes. I
pass a node set to a style sheet and how to resolve external style sheets using XmlResolver.
Introduction
xix
03_596772 flast.qxd 12/13/05 11:17 PM Page xix
XML Support in ADO.NET
In Chapter 8, I start to move away from the realm of the System.Xml namespace of classes, to explore the
broader picture of how XML is used in .NET specifically from ADO.NET, the data access technology of
choice.
Chapter 8 looks at the role of XML in ADO.NET 2.0 and highlights the new XML related features of
ADO.NET. I cover the capabilities of the DataSet and DataTable classes, including reading and writing
XML, and programmatically accessing or changing its XML representation. I highlight how to synchro-
nize DataSets with XmlDataDocuments and why you would do so. I also cover the creation of strongly
typed DataSets and their advantages. Finally, I take a glimpse at how to access some of the new XML
features available in SQL Server 2005 from ADO.NET.
XML Data Display
The XML support in ASP.NET provides excellent support for storing, retrieving and rendering XML.
I start with looking at the new web.sitemap file that allows you to store the hierarchy of a Web site and
leverage that to drive the navigation structure of a Web site. Then, I go on to discuss the features of new
XML data controls such as XmlDataSource, TreeView, and GridView for consuming and displaying
native XML directly in the browser. Finally, I also introduce the new ASP.NET 2.0 script callback feature
for retrieving XML data directly from the browser without refreshing the page.
SQL Server 2005 XML Integration
With the release of SQL Server 2005, XML support just got better and SQL Server 2005 provides powerful
XML query and data modification capabilities over XML data. To start with, I introduce the new XML
features of SQL Server 2005 including the FOR XML clause enhancements, XQuery support, and the
XML data type. Then I go on to discuss the execution of FOR XML queries from within ADO.NET both
synchronously and asynchronously. I also discuss the steps involved in working with typed and
untyped XML data type columns. Finally, I illustrate how to retrieve XSD schemas from a typed column
using ADO.NET and also focus on MARS and OPENXML() functions.
Building an Airline Reservation System using ASP.NET 2.0
them from your ASP.NET applications.
Building a ShoppingAssistant using XML Web Services
This chapter is based on a case study named ShoppingAssistant, which provides one stop shopping for
consumers that want to find out information such as the products that are on sale, availability of prod-
ucts in different stores, comparison of the price of the product across different stores and so on. In this
case study, I demonstrate how to leverage Web services in a real world Web application by using asyn-
chronous Web service invocation capabilities in conjunction with other .NET features such as XML
Serialization, FileSystemWatcher, and Timer component.
What You Need to Use This Book
All of the examples in this book are ASP.NET samples. The key requirements for running these applica-
tions are the .NET Framework 2.0 and Microsoft Visual Studio 2005. You also need to have SQL Server
2005 server along with the AdventureWorks sample database installed to make most of the samples
work. A few examples make use of SQL Server 2005 Express database.
The SQL Server examples in this book utilize integrated security to connect to the SQL Server database,
so remember to enable integrated authentication in your SQL Server. This will also require you to turn
on integrated Windows authentication (as well as impersonation depending on your configuration) in
ASP.NET Web sites.
Conventions
To help you get the most from the text and keep track of what’s happening, I’ve used a number of con-
ventions throughout the book.
Introduction
xxi
03_596772 flast.qxd 12/13/05 11:17 PM Page xxi
Tips, hints, tricks, and asides to the current discussion are offset and placed in italics like this.
As for styles in the text:
❑ We highlight new terms and important words when we introduce them.
❑ We show keyboard strokes like this: Ctrl+A.
❑ We show file names, URLs, and code within the text like so:
persistence.properties.
Source Code
xxii
03_596772 flast.qxd 12/13/05 11:17 PM Page xxii
If you don’t spot “your” error on the Book Errata page, go to www.wrox.com/contact/techsupport
.shtml
and complete the form there to send us the error you have found. We’ll check the information
and, if appropriate, post a message to the book’s errata page and fix the problem in subsequent editions
of the book.
p2p.wrox.com
For author and peer discussion, join the P2P forums at p2p.wrox.com. The forums are a Web-based sys-
tem for you to post messages relating to Wrox books and related technologies and interact with other
readers and technology users. The forums offer a subscription feature to e-mail you topics of interest of
your choosing when new posts are made to the forums. Wrox authors, editors, other industry experts,
and your fellow readers are present on these forums.
At
you will find a number of different forums that will help you not only as
you read this book, but also as you develop your own applications. To join the forums, just follow these
steps:
1. Go to p2p.wrox.com and click the Register link.
2. Read the terms of use and click Agree.
3. Complete the required information to join as well as any optional information you wish to pro-
vide and click Submit.
4. You will receive an e-mail with information describing how to verify your account and com-
plete the joining process.
You can read messages in the forums without joining P2P but in order to post your own messages, you
must join.
Once you join, you can post new messages and respond to messages other users post. You can read mes-
sages at any time on the Web. If you would like to have new messages from a particular forum e-mailed
to you, click the Subscribe to this Forum icon by the forum name in the forum listing.
For more information about how to use the Wrox P2P, be sure to read the P2P FAQs for answers to ques-
tions about how the forum software works as well as many common questions specific to P2P and Wrox