Table Of Contentwww.it-ebooks.info
Applied XML Programming for Microsoft .NET
Dino Esposito
Microsoft Press
A Division of Microsoft Corporation One Microsoft Way Redmond, Washington 98052-6399
Copyright © 2003 by Dino Esposito
All rights reserved. No part of the contents of this book may be reproduced or
transmitted in any form or by any means without the written permission of the publisher.
Library of Congress Cataloging-in-Publication Data [ pending.]
Esposito, Dino, 1965-
Applied XML Programming for Microsoft .NET / Dino Esposito
p. cm.
Includes index.
ISBN 0-7356-1801-1
1. XML (Document markup language) 2. Microsoft .NET. I. Title.
QA76.76.H94 E85 2002
005.7'2--dc21 2002029546
Printed and bound in the United States of America.
1 2 3 4 5 6 7 8 9 QWT 7 6 5 4 3 2
Distributed in Canada by H.B. Fenn and Company Ltd.
A CIP catalogue record for this book is available from the British Library.
Microsoft Press books are available through booksellers and distributors worldwide. For
further information about international editions, contact your local Microsoft Corporation
office or contact Microsoft Press International directly at fax (425) 936-7329. Visit our
Web site at www.microsoft.com/mspress. Send comments to:
<mspinput@microsoft.com>.
ActiveX, IntelliSense, JScript, Microsoft, Microsoft Press, MS-DOS, Visual Basic, Visual
Studio, Win32, Windows and Windows NT are either registered trademarks or
trademarks of Microsoft Corporation in the United States and/or other countries. Other
product and company names mentioned herein may be the trademarks of their
respective owners.
The example companies, organizations, products, domain names, e-mail addresses,
logos, people, places, and events depicted herein are fictitious. No association with any
real company, organization, product, domain name, e-mail address, logo, person,
place, or event is intended or should be inferred.
Acquisitions Editor: Anne Hamilton
Project Editor: Lynn Finnel
Technical Editor: Marc Young
Body Part No. X08-81851
www.it-ebooks.info
Dino Esposito
Dino Esposito is Wintellect's ADO.NET and XML expert and a trainer and consultant
who specializes in .NET and Web applications. A frequent speaker at popular industry
events such as Microsoft TechEd, VSLive!, DevConnections, and WinSummit, Dino is
also a prolific author writing the monthly "Cutting Edge" column for MSDN Magazine
and the "Diving into Data Access" column for MSDN Voices. He also regularly
contributes to a number of other magazines, including Visual Studio Magazine, CoDe
Magazine, and asp.netPRO Magazine (http://www.aspnetpro.com). During a few rare
moments of spare time, Dino cofounded http://www.vb2themax.com, a Web site for
Visual Basic and Visual Basic .NET developers.
Fond of sea and beaches, Dino lives in Italy, precisely in the Rome area, with his wife,
Silvia, and two children—Francesco and Michela.
To Silvia, Francesco, and Michela
Acknowledgments
I can say it now: Several times I was about to start an XML book project, but then for
one reason or another the project never took off. So I'd like to start by saying thanks to
the people who believed in a fairly confused book idea and worked to make it happen.
These people are Anne Hamilton and Jeannine Gailey. (By the way, all the best,
Jeannine!)
Lynn Finnel brought the usual fundamental contribution as project editor. As Lynn
originally described her role in the first e-mail we exchanged, being an editor is a
delicate art, as you have to reconcile the needs of many people while meeting your own
deadlines. Thanks again, Lynn.
And a warm thanks goes to Jennifer Harris, who edited the book, and technical
reviewers Marc Young, Jim Fuchs, Julie Xiao, and Jean Ross.
Other people were involved with this book, mostly as personal reviewers. Francesco
Balena tested some of the code and provided a lot of insight. In particular, Giuseppe
Dimauro and Giuseppe Guerrasio helped to figure out the intricacies of the
XmlSerializer class, and Ralph Westphal did the same with custom readers. Kenn
Scribner has been the ideal extension to the MSDN documentation about Web
www.it-ebooks.info
services. Rainer Heller of Siemens offered a really interesting perspective on Web
services interoperability. It was nice to discuss Web services in the more general
context of a conversation based on the World Football Championships—an indirect
demonstration that Web services are still interoperable today!
Thanks to all the Wintellect guys, and Jason Clark and Jeffrey Richter, in particular, for
their friendly and effective support.
And now my family. I've noticed that many authors, when writing acknowledgments,
promise their families that they will never repeat the experience. Although rewarding for
themselves, they explain, writing a book is too hard on the rest of the family to be
repeated. I'll be honest and sincere here. So, Silvia, and Francesco and Michela, set
your mind at rest. I will do all I can to write even more books. But I love you all beyond
imagination.
—'til the next book
Dino
www.it-ebooks.info
Table of Contents
Applied XML Programming for Microsoft .NET
Introduction
Part I - XML Core Classes in the .NET Framework
Chapter 1 - The .NET XML Parsing Model
Chapter 2 - XML Readers
Chapter 3 - XML Data Validation
Chapter 4 - XML Writers
Part II - XML Data Manipulation
Chapter 5 - The XML .NET Document Object Model
Chapter 6 - XML Query Language and Navigation
Chapter 7 - XML Data Transformation
Part III - XML and Data Access
Chapter 8 - XML and Databases
Chapter 9 - ADO.NET XML Data Serialization
Chapter 10- Stateful Data Serialization
Part IV - Applications Interoperability
Chapter 11- XML Serialization
Chapter 12- The .NET Remoting System
Chapter 13- XML Web Services
Chapter 14- XML on the Client
Chapter 15- .NET Framework Application Configuration
Afterword
Index
List of Figures
List of Tables
List of Sidebars
www.it-ebooks.info
Introduction
It was about five years ago, a few days after I finished my first book, when the publisher
came to me with a rather enticing proposal: "Why don't you start thinking about a new
book?" Now I realize that all publishers make this sort of proposition, but at the time the
proposal was definitely alluring, and a clear signal—I thought—of appreciation.
"Because you seem to do so well with new technologies," they said, "we'd like you to
have a look at this new stuff called XML." It was the first time I had heard about XML,
which was not yet a W3C recommendation.
A lot of things have happened in the meantime, and XML did go a long way. You can
be sure that, as I write this, a thousand or more IT managers are giving presentations
that include XML in one way or another. Not many years ago, at a software conference,
I heard a product manager emphasize the key role played by XML in the suite of
products he was presenting. After the first dozen sentences to the effect that "this
feature wouldn't have been possible without XML," one of the attendees asked a candid
question: "Is there a function in which you didn't use XML?" The presenter's genuine
enthusiasm led everyone there (including myself) to believe that programming would no
longer be possible without a strong knowledge of XML. We were more than a little
reassured by the speaker's answer: "Oh no, we didn't use XML in the compiler."
Regardless of the hype that often accompanies it, XML truly is a key element in
software. Today, XML is more than just a software technology. XML is a fundamental
aspect of all forms of programming, as essential as water and air to every human being.
Just as human beings realistically need some infrastructure to take advantage of water
and air, programming forms of life must be supported by software tools to be effective
and express their potential in terms of interoperability, flexibility, and information. For
XML, the most important of these tools is the parser.
An XML parser reads in XML text and outputs a memory representation of the contents.
The input for an XML parser is always plain and platform-independent text, although
potentially encoded in a variety of character sets, whereas the output of an XML parser
is strictly tied to the underlying hardware and software platform. Depending on the
operating system and the programming environment of choice, an XML parser can
generate a Component Object Model (COM) object as well as a Java or a JScript class.
No matter the kind of output, however, the end result is XML data in a programmable
form.
The growing level of integration and orchestration that partner applications need makes
the exchanged XML code more and more sophisticated and often requires the use of
specialized dialects like Simple Object Access Protocol (SOAP) and XPath. As a result,
XML programming requires ad hoc tools for reading and writing in these dialects; all the
better if the tools are tightly integrated into some sort of programming framework.
Effective XML programming requires that you be able to generate XML in a more
powerful way than merely concatenating strings. The XML API must be extensible
enough to accommodate pluggable technologies and custom functionalities. And it
must be serializable and integrate well with other elements of data storage and
exchange, including databases, complex data types (arrays, tables, and lists), and—
why not?—visual user interface elements. In simple terms, XML must no longer be a
distinct API bolted onto the core framework, but instead be a fully integrated member of
the family. This is just what XML is in the Microsoft .NET Framework. And this book is
about XML programming with the .NET Framework.
1
www.it-ebooks.info
What Is This Book About?
This book explores the array of XML tools provided by the .NET Framework. XML is
everywhere in the .NET Framework, from remoting to Web services, and from data
access to configuration. In the first part of this book, you'll find in-depth coverage of the
key classes that implement XML in the .NET platform. Readers and writers, validation,
and schemas are discussed with samples and reference information. Next the book
moves on to XPath and XSL Transformations (XSLT) and the .NET version of the XML
Document Object Model (XML DOM).
The final part of this book focuses on data access and interoperability and touches on
SQL Server 2000 and its XML extensions and .NET Remoting and its cross-platform
counterpart—XML Web services. You'll also find a couple of chapters about XML
configuration files and XML data islands and browser/deployed managed controls.
What Does This Book Cover?
This book attempts to answer the following common questions:
(cid:131) Can I read custom data as XML?
(cid:131) What are the guidelines for writing custom XML readers?
(cid:131) Is it possible to set up validating XML writers?
(cid:131) How can I extend the XML DOM?
(cid:131) Why should I use the XPath navigator object whenever possible?
(cid:131) Can I embed my own managed classes in an XSLT script?
(cid:131) How can I serialize a DataSet object efficiently?
(cid:131) What is the DiffGram format?
(cid:131) Are the SQL Server 2000 XML Extensions (SQLXML) worth using?
(cid:131) Why does the XML serializer use a dynamic assembly?
(cid:131) When should I use Web services instead of .NET Remoting?
(cid:131) How can I embed managed controls in Web pages?
(cid:131) How can managed controls access client-side XML data islands?
(cid:131) How do I insert my own XML data in a configuration file?
All of the sample files discussed in this book (and even more) are available through the
Web at the following address: http://www.microsoft.com/mspress/books/6235.asp. To
open the Companion Content page, click on the Companion Content link in the More
Information box on the right side of the page.
Although all the code shown in this book is in C#, the sample files are available both in
C# and in Microsoft Visual Basic .NET. Here are some of the more interesting
examples:
(cid:131) An XML reader that reads CSV files and exposes their contents as XML
(cid:131) An extended version of the XML DOM that detects changes to the disk file and
automatically refreshes its data
(cid:131) A Web service that offers dynamically created images
(cid:131) An XML reader class with writing capabilities
(cid:131) A class that serializes DataTable objects in a true binary format
(cid:131) A tool to track the behavior of the XML serializer class
(cid:131) A ListView control that retrieves its data from the host HTML page
These and other samples will get you on your way to XML in the .NET Framework.
2
www.it-ebooks.info
What Do I Need to Use This Book?
Most of the examples in this book are Windows Forms or console applications. The key
requirements for running these applications are the .NET Framework and Microsoft
Visual Studio .NET. You also need to have SQL Server 2000 installed to make most of
the samples work, and a few examples make use of Microsoft Access 2000 databases.
The SQLXML 3.0 extensions are required for the samples in Chapter 8. The code has
been tested with the .NET Framework SP1.
The SQL Server examples in this book assume that the sa account uses a blank
password, although the use of such a blank password is strongly discouraged in any
professional development environment. If your SQL Server sa account doesn't use a
blank password, you'll need to add the sa password to the connection strings in the
source code. For example, if your sa password is "Hello", the following connection
string provides access to the Northwind database:
string nwind =
"SERVER=localhost;UID=sa;pswd=Hello;DATABASE=northwind;";
Some of the applications in this book require SOAP Toolkit 2.0 and SQLXML 3.0.
These products are available at the following locations:
(cid:131) SOAP Toolkit 2.0
http://msdn.microsoft.com/downloads/default.asp?URL=/downloads/sample.
asp?url=/MSDN-FILES/027/001/580/msdncompositedoc.xml
(cid:131) SQLXML 3.0
http://msdn.microsoft.com/downloads/default.asp?URL=/downloads/sample.
asp?url=/MSDN-FILES/027/001/824/msdn-compositedoc.xml
Contacting the Author
Please feel free to send any questions about this book directly to the author. Dino
Esposito can be reached via e-mail at one of the following addresses:
(cid:131) <dinoe@wintellect.com>
(cid:131) <desposito@vb2themax.com>
In addition, you can contact the author at the Wintellect (http://www.win-tellect.com) and
VB2-The-Max (http://www.vb2themax.com) Web sites.
Support
Every effort has been made to ensure the accuracy of this book and the contents of the
sample files. Microsoft Press provides corrections for books through the Web at the
following address:
http://www.microsoft.com/mspress/support/
To connect directly to the Microsoft Press Knowledge Base and enter a query regarding
a question or issue that you might have, go to:
http://www.microsoft.com/mspress/support/search.asp
If you have comments, questions, or ideas regarding this book or the sample files,
please send them to Microsoft Press using either of the following methods:
Postal mail:
3
www.it-ebooks.info
Microsoft Press
Attn:Microsoft .NET XML Programming Editor
One Microsoft Way
Redmond, Wa 98052-6399
E-mail:
<MSPINPUT@MICROSOFT.COM>
Please note that product support is not offered through the above mail addresses. For
support information, please visit the Microsoft Product Support Web site at
http://support.microsoft.com
4
www.it-ebooks.info
Part I:XML Core Classes in the .NET Framework
Chapter List
Chapter 1: The .NET XML Parsing Model
Chapter 2: XML Readers
Chapter 3: XML Data Validation
Chapter 4: XML Writers
Part Overview
5
www.it-ebooks.info