Tài liệu Applied XML Programming for Microsoft .NET docx

537 538 0
Tài liệu Applied XML Programming for Microsoft .NET docx

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

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 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 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 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 1 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. 2 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:  Can I read custom data as XML?  What are the guidelines for writing custom XML readers?  Is it possible to set up validating XML writers?  How can I extend the XML DOM?  Why should I use the XPath navigator object whenever possible?  Can I embed my own managed classes in an XSLT script?  How can I serialize a DataSet object efficiently?  What is the DiffGram format?  Are the SQL Server 2000 XML Extensions (SQLXML) worth using?  Why does the XML serializer use a dynamic assembly?  When should I use Web services instead of .NET Remoting?  How can I embed managed controls in Web pages?  How can managed controls access client-side XML data islands?  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:  An XML reader that reads CSV files and exposes their contents as XML  An extended version of the XML DOM that detects changes to the disk file and automatically refreshes its data  A Web service that offers dynamically created images  An XML reader class with writing capabilities  A class that serializes DataTable objects in a true binary format  A tool to track the behavior of the XML serializer class  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. 3 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:  SOAP Toolkit 2.0 http://msdn.microsoft.com/downloads/default.asp?URL=/downloads/sample. asp?url=/MSDN-FILES/027/001/580/msdncompositedoc.xml  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:  <dinoe@wintellect.com>  <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: 4 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 5 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 [...]... .NET Linux Win32 Java The corresponding node layout that we want to extrapolate consists of a block of XML data that comprises all the element nodes of the source file, as shown here: ... available 20 Chapter 2: XML Readers In the Microsoft NET Framework, two distinct sets of classes provide for XML- driven reading and writing operations These classes are known globally as XML readers and writers The base class for readers is XmlReader, whereas XmlWriter provides the base programming interface for writers In this chapter, we'll focus on a particular type of XML readers—the XML text readers... representation as an XML document For more information, refer to http://www.w3.org/TR/2002/WD-DOM-Level-3Core-20020409 A known parser that already provides an experimental implementation of DOM Level 3 Core is IBM's XML Parser for Java (Xml4 J) See http://www.alphaworks.ibm.com/tech /xml4 j for more information Core Classes for Parsing Regardless of the underlying platform, the available XML parsers fall... represents an excellent parser for the Windows platform and has been updated to support W3C final recommendations for the XML Schema COM and NET Framework XML Core Services The first difference between MSXML and NET Framework XML core classes that catches the eye is the fact that while MSXML supports XMLDOM and SAX parsers, the NET Framework supplies an XMLDOM parser and XML readers and writers (More... source file, as shown here: 28 To produce these results, I created the GetXmlFileNodeLayout function This function scans the entire contents of the XML file and processes each node found along the way Only two types of nodes are relevant for this example: the start and end tags of Element... In MSXML 4.0, Microsoft introduced the same level of support for some relatively newer XML standards that are found in NET Framework XML core classes—in particular, XSD, the XML Schema object model, and XPath If you look at MSXML 3.0, however, the differences between managed and unmanaged XML processing are clearer Using MSXML in the NET Framework As with other COM objects, you can import the MSXML... Finally, if you just want a good, all-encompassing book about XML programming, I heartily recommend the Microsoft Press Core Reference book XML Programming (http://www .microsoft. com/mspress/books/4798.asp), by R Allen Wyke, Sultan Rehman, and Brad Leupen (Microsoft Press, 2002) For a more general look into XML as a unifying technology, Essential XML: Beyond Markup (Addison Wesley, 2000), by Don Box, Aaron... of the XMLDOM parser Two abstract classes—XmlReader and XmlWriter—are at the very foundation of all NET Framework XML classes, including XMLDOM classes, ADO.NET-related classes, and configuration classes So in the NET Framework you have two possible approaches when it comes to processing XML data You can use either any classes directly built onto XmlReader and XmlWriter or classes that expose information... on to XML writers in Chapter 4 The Programming Interface of Readers XmlReader is an abstract class available from the System .Xml namespace It defines the set of functionalities that an XML reader exposes to let developers access an XML stream in a noncached, forward-only, read-only way An XML reader works on a read-only stream by jumping from one node to the next in a forward-only direction The XML reader... 7 examine the related standards, such as XPath and XSL XML and ADO.NET The interaction between ADO.NET classes and XML documents takes one of two forms: Serialization of ADO.NET objects (in particular, the DataSet object) to XML documents and corresponding deserialization Data can be saved to XML in a variety of formats, with or without schema information, as a full snapshot of the in-memory data including . 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. 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.

Ngày đăng: 11/12/2013, 14:15

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan