
- •maranGraphics
- •CREDITS
- •ACKNOWLEDGMENTS
- •ABOUT THE AUTHORS
- •AUTHORS’ ACKNOWLEDGMENTS
- •TABLE OF CONTENTS
- •HOW TO USE THIS BOOK
- •INTRODUCTION TO C#
- •START VISUAL STUDIO .NET
- •OPEN A NEW C# PROJECT
- •OPEN A C# WEB PROJECT
- •SET JSCRIPT .NET AS THE DEFAULT SCRIPT LANGUAGE
- •EXPLORE THE CLASS VIEW WINDOW
- •VIEW THE CONTENTS WINDOW
- •GET HELP USING THE INDEX WINDOW
- •SEARCH FOR HELP
- •ADD COMPONENTS FROM THE TOOLBOX
- •ADD A TASK TO THE TASK LIST
- •CHANGE FORM PROPERTIES IN THE PROPERTIES WINDOW
- •ADD A CUSTOM TOOLBAR
- •DELETE A TOOLBAR
- •CHANGE THE VISUAL STUDIO ENVIRONMENT
- •MANAGE OPEN WINDOWS
- •OPEN A PROJECT
- •VIEW THE MAIN METHOD
- •COMBINE PROGRAM TYPES
- •ADD REFERENCE TYPES
- •ADD OPERATORS
- •INSERT ATTRIBUTES
- •ENTER CLASSES
- •ADD COMMENTS TO CODE
- •WRITE YOUR FIRST PROGRAM
- •ENTER XML DOCUMENTATION
- •ACCESS DOCUMENTATION
- •LOG A BUG REPORT
- •VIEW INFORMATION ABOUT C# BUILDING BLOCKS
- •PROGRAM CLASSES
- •ADD A CLASS
- •EMPLOY CLASS INHERITANCE
- •PROGRAM INSTANCE CONSTRUCTORS
- •INSERT DESTRUCTORS
- •PROGRAM STRUCTS
- •DISPLAY HEAP AND STACK INFORMATION
- •FIND TYPE INFORMATION
- •PROGRAM CONSTANT EXPRESSIONS
- •SPECIFY VALUE TYPES
- •PROGRAM NUMERIC TYPES
- •PROGRAM THE BOOLEAN TYPE
- •DECLARE REFERENCE TYPES
- •ENTER REFERENCE TYPE DECLARATIONS
- •CONVERT VALUE TYPES TO REFERENCE TYPES
- •PROGRAM POINTER TYPES
- •INSERT THE VOID TYPE
- •ADD INTERFACE PROPERTIES
- •ADD AN INTERFACE INDEX
- •VIEW INFORMATION ABOUT METHODS
- •ADD A METHOD
- •ADD STATIC METHODS
- •INCLUDE NON-STATIC METHODS
- •ENTER DELEGATES
- •PROGRAM EVENTS
- •ADD AN EVENT-HANDLING METHOD
- •VIEW INFORMATION ABOUT ARRAYS
- •ENTER SINGLE-DIMENSIONAL ARRAYS
- •ADD MULTIDIMENSIONAL ARRAYS
- •PROGRAM ARRAY-OF-ARRAYS
- •ITERATE THROUGH ARRAY ELEMENTS
- •SORT ARRAYS
- •SEARCH ARRAYS
- •IMPLEMENT A COLLECTIONS CLASS
- •PROGRAM STRUCTS
- •ADD AN INDEXER
- •INCLUDE ENUMERATIONS
- •CREATE STRING LITERALS AND VARIABLES
- •ASSIGN VALUES TO STRINGS
- •CONCATENATE STRINGS
- •COMPARE STRINGS
- •SEARCH FOR SUBSTRINGS
- •REPLACE CHARACTERS
- •EXTRACT SUBSTRINGS
- •CHANGE THE CHARACTER CASE
- •TRIM SPACES
- •REMOVE CHARACTERS
- •SPLIT A STRING
- •JOIN STRINGS
- •PAD STRINGS
- •VIEW INFORMATION ABOUT PROPERTIES
- •COMPARE PROPERTIES AND INDEXERS
- •PROGRAM PROPERTY ACCESSORS
- •DECLARE ABSTRACT PROPERTIES
- •INCLUDE PROPERTIES ON INTERFACES
- •VIEW INFORMATION ABOUT WINDOWS FORMS
- •ADD A WINDOWS FORM IN THE WINDOWS FORM DESIGNER
- •SET THE FORM TYPE
- •CHOOSE THE STARTUP WINDOWS FORM
- •CREATE A MODAL FORM
- •LAYOUT A FORM
- •SET A FORM LOCATION
- •CHANGE FORM PROPERTIES
- •CREATE A TRANSPARENT FORM
- •AN INTRODUCTION TO WEB FORMS AND CONTROLS
- •CREATE AN ASP.NET WEB SITE
- •CREATE A WEB FORM
- •ADD SERVER CONTROLS TO A WEB FORM
- •READ AND CHANGE PROPERTIES FROM OBJECTS ON A WEB FORM
- •USING SERVER-SIDE COMPONENTS ON WEB FORMS
- •INTRODUCING DATA ACCESS WITH ADO.NET
- •DISPLAY DATA WITH THE DATAGRID CONTROL
- •CONFIGURE THE DATAGRID CONTROL
- •INSERT DATA INTO A SQL DATABASE
- •UPDATE DATA FROM A SQL DATABASE
- •DELETE DATA FROM A SQL DATABASE
- •EXECUTE A STORED PROCEDURE IN A SQL DATABASE
- •READ XML FROM A FILE
- •SAVE XML TO A FILE
- •QUERY XML WITH XPATH
- •APPLY XSL TO XML
- •INTRODUCTION TO DISTRIBUTED APPLICATIONS
- •CREATE AN APPLICATION WITH PRIVATE ASSEMBLIES
- •CREATE AN APPLICATION WITH SHARED ASSEMBLIES
- •VERSION A SHARED ASSEMBLY
- •CONFIGURE A CLIENT FOR A VERSIONED ASSEMBLY
- •CREATE A WEB SERVICE
- •USING A WEB SERVICE
- •INTRODUCTION TO EXCEPTION HANDLING
- •THROWING AN EXCEPTION
- •HANDLING EXCEPTIONS WITH THE CATCH BLOCK
- •USING THE FINALLY BLOCK
- •WRITE ERRORS TO THE APPLICATION LOG
- •BASIC EXAMPLES
- •WHAT’S ON THE CD-ROM
- •USING THE E-VERSION OF THIS BOOK
- •INDEX
- •Symbols & Numbers

C#
APPLY XSL TO XML
XML documents are a good choice for transportable data, but may contain more data than is necessary for your application. To retrieve only a portion of the
XML data, you can transform a source XML document into another XML document by using an XSLT transformation. The resulting document does not always have to be XML. In some cases, you use XSLT transformations to create HTML documents.
XSLT is a language for transforming source XML documents into other document formats using XPath or XSLT as the query language. You can use the XslTransform class, which is part of the System.Xml.Xsl namespace to
orchestrate XSLT transformations. To build well-performing XSLT transformations, you can use an XPathDocument as the XSLT data store. If you are working with a DataSet, you can use XmlDataDocument as your source file in a transformation.
To map the XslTransform class to an XSLT style sheet, you can use the Load method. When you execute the Transform method of the XslTransform class, there are several overload options. In the steps that follow, the Transform method writes the XML to a file.
APPLY XSL TO XML
‹ Rename the namespace to ApplyXSL.
› Rename the class name to ApplyXSL.
ˇ Save the file.
Á Add the Main function.
‡ Create an
XslTransform variable.
° Use the Load function to load the style sheet.
· Use the Transform function to transform the XML document using the XSL style sheet.
‚ Press F5 to save, build, and run the console application.

USING THE XML FRAMEWORK CLASS 13
For faster transformations, load your XML into an XPathDocument.
To run this sample, you need to put the XML and XSL source documents in the working directory of your EXE file.
TYPE THIS:
using System; using System.Xml; using System.Xml.Xsl; using System.Xml.XPath; namespace ApplyXSL{
class ApplyXSL { static void Main(){
XPathDocument xpdLibrary = new XPathDocument ("photo_library.xml"); XslTransform xsltFavorites = new XslTransform(); xsltFavorites.Load("favorite.xsl");
XmlReader reader = xsltFavorites.Transform(xpdLibrary, null); while (reader.Read()) {
// With each node write to the console. (Look at cd for full code.)
}
}}}
RESULT:
C:\>csc ApplyXSL_ai.cs
C:\> ApplyXSL_ai.exe
"Screen will echo out the nodes in the document.
Including the type node, name, and contents."
C:\>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
— Open the style sheet and |
|
± Open the XML document |
■ The resulting XML |
||||||
|
|
|||||||||
review the contents of the |
that was created from |
document appears. |
||||||||
style sheet. |
the transform. |
|
|
|
251

C#
INTRODUCTION TO DISTRIBUTED APPLICATIONS
he .NET Framework is Microsoft’s new computing Tplatform designed to simplify application development
in the highly distributed environment of the Internet. Microsoft has put a major effort in revamping the architecture for their component-based solutions. When you create applications on the .NET platform, you find component development tightly integrated with the solutions you build.
Most application development solutions benefit from creating component-based solutions. The .NET platform enables you to take a very simple approach to distributed component-based solutions by using private assemblies.
By using private assemblies, you can reap the benefits of component programming without the headaches of dealing with versions that are not backward-compatible. Also, it is easier to control your component and how those components get versioned into existing deployed applications.
With highly reuseable components, you can create shared assemblies. Shared assemblies give you more control with your components, but for a price. Shared assemblies enable you to share components across applications, to version your component, and to localize components, among other capabilities.
EVOLUTION OF COM AND DCOM TO .NET
Applications that use components have proven to be an effective way to build applications. For Microsoft, the open standard for component development started in 1993 with the introduction of the Component Object Model, or COM. Microsoft further enhanced COM into a distributed model with DCOM, Distributed COM, in 1996. Used on more than 150 million systems worldwide today, COM is widely accepted and heavily leveraged in enterprise application for many Fortune 500 companies. The most recent version that is integral to Windows 2000 is COM+. COM+ was an integration of Microsoft Transaction Server (MTS) and COM.
COM/COM+ is the backbone for Microsoft’s Distributed interNet Applications (DNA) platform.
Despite Microsoft’s success with DNA, they are evolving to a new framework. With a mature framework, like DNA via COM, there are issues that cannot be properly addressed due to preserving compatability with earlier versions of COM. .NET takes the issues of what COM+ has today and addresses them based on the best of the COM+ runtime and what other competitive component runtimes have to offer.
DLL HELL
The .NET platform addresses one of the major issues of DNA applications, DLL Hell. This refers to the problems that occur when multiple applications attempt to share a COM class. COM enables one or more clients to share classes in COM components. When one client application updates an existing COM component that is not backward-compatible with the version already on the machine, the first client breaks when it tries to create a component based on the new class that is not backward-compatible.
.NET addresses the issue of DLL Hell with side-by-side execution of components via use of assemblies. .NET can perform side-by-side execution of components.
252

CREATING AND DEPLOYING DISTRIBUTED APPLICATIONS 14
USING VERSIONING IN .NET
Versioning takes on a new meaning with .NET. With COM components, you register a component for reuse by putting several entries into the Windows Registry, a proprietary store where Windows holds application and operating system settings. The entries in the Windows Registry can end up being corrupted by bad development practices, causing applications to fail when calling the component that has corrupted Registry entries.
With .NET, the versioning has more capabilities and is easier to control. .NET uses the version number when determining which build of a class to load. Configuring what build is used is easily configured through the config file, class, for your application. See page 266 to learn about binding a component version in the
AssemblyInfo project file.
USING ASSEMBLIES AND GAC
The .NET platform addresses the DLL Hell issue with assemblies. Assemblies enable you to register more than one version of the same component on the same machine. Note that the word register does not mean using the Windows Registry. When you register a version, the version resides in the machine’s Global Assembly Cache, or GAC. Items in the GAC are shared assemblies that multiple clients can use. Assemblies that exist in the GAC have a version number assigned to them. When a client calls for a component, the GAC assists in matching the client component request with the correct version of the component, not just the last installed version. With the capability of Global
Assemblies, you can have two versions of the same component running on the same machine, also called side-by-side execution.
The .NET platform considers components not in the GAC as private assemblies and packages them in the client’s application directory. You can also configure your private assemblies to exist in one of the subdirectories of the application directory. You do not have the benefit of sharing private assemblies across multiple machines, but you can deploy them very simply using xcopy, an old command-line utility that enables you to copy multiple files at the same time.
USING NAMESPACES IN THE .NET PLATFORM
In the .NET platform, you see the use of namespaces for identifying objects. All examples presented in this book illustrate the use of the keyword Namespace in the classes. When you compile a project, you use namespaces to organize the classes defined in the resulting assembly. Assemblies can contain multiple
namespaces, which can in turn contain other namespaces. Namespaces assist in providing uniqueness and simplify references when using large groups of objects such as class libraries. You can use aliases if you want to avoid fully qualifying a class nested in a namespace.
HOW WEB SERVICES FIT IN
Web Services are a big part of the distributed model for .NET. Web Services basically provide a programmable entity, such as application logic or data, via the Internet standards such as XML and HTTP. Web Services expose your systems to the Internet to yield highly distributed applications that can interact with
other systems regardless of the operating system or programming language. Web Services meet the challenge of an ultimate heterogeneous environment; it is accessable over the Internet and agnostic regarding the choice of operating system, object model, or programming language.
253