NET Type Mapping

All the data types that can be used in XSD documents have a .NET Framework counterpart. After an XSD has been compiled into a .NET Framework representation object model, you can access it using the SOM classes. I'll have more to say on this in the section Modifying a Schema Programmatically, on page 123. The infoset that results from the schema compilation is also defined in the XSD recommendation and is said to be the post-schema-validation infoset (PSVI). The SOM renders the PSVI fields using...

Adding Type Information

One of the constructors of the XmlSerializerclass takes a second argument of type XmlTypeMapping. The XmlSerializer class is used to encode and serialize an object to SOAP. The following code is used to add XSD type definitions to a serialized class SoapReflectionImporter imp new SoapReflectionImporter() XmlTypeMapping tm XmlSerializer ser new XmlSerializer(tm) Let's assume the following class definition The typed XML output looks like this < xml version 1.0 encoding utf-8 > < Employee...

From XML to HTML

Let's return to our faithful XML document (data.xml) from previous chapters and turn it into a compelling HTML page. This sample XML document contains information about the employees in the Northwind database's Employees table. The idea is to create a final HTML page that renders the information about employees through a table. The structure of the XSLT script is shown in the following code < xsl template match > < HTML> < BODY> < H1> Northwind's Employees< H1> < TABLE>...

The ADO Style Sheet

Let's analyze the XSLT code necessary to transform a DataSet object into the XML version of an ADO Recordset object. The following listing shows the over-all layout < xsl stylesheet version 1.0 < xsl output method xml > < -- Matches the DataSet's root, whatever the name --> < xsl template < -- PARAM Name of the table to consider --> < xsl param name TableName select string('Table') > < The XML-based ADO Recordset > < End of the XML-based ADO Recordset > < xsl...

Complex Type Inheritance

With complex types, you simply define XML data structures that are in no logical way different from classes of object-oriented languages such as C or Java. One key feature of those languages is the ability to derive new data types from existing classes. The same kind of inheritance can be achieved with XML schemas. To demonstrate, we'll build a new address type that, as in many European countries, takes into account also the province. The address.xsd schema considered up to now contains more...

The Csv Xml Reader in Action

In this section, you'll see the CSV XML reader in action and learn how to instantiate and use it in the context of a realistic application. In particular, I'll show you how to load the contents of a CSV file into a DataTable object to appear in a Windows Forms DataGrid control. Figure 2-1 shows the application in action. Figure 2-1 The CSV XML reader shows all the rows of a CSV file. You start by instantiating the reader object, passing the name of the CSV file to be processed and a Boolean...

Validating Against an XSD Document

After this long digression into the XML Schema API in the .NET Framework, let's conclude this chapter by looking at what happens when the XmlValidatingReader class is called to operate on an XML file that includes, or references, an XML schema. The following code shows how to set up the XML validator class to work on XSD files XmlTextReader coreReader new XmlTextReader(fileName) reader.ValidationType ValidationType.Schema XmlValidatingReader XmlValidatingReader When the ValidationType property...

Using GDI to Create Charts

GDI+ is the latest incarnation of the classic Windows Graphical Device Interface (GDI), a graphics subsystem that enables you to write device-independent applications. The .NET Framework encapsulates the full spectrum of GDI+ functionalities in quite a few managed classes that wrap any GDI+ low-level functions, thus making them available to Web Forms and Windows Forms applications. GDI+ services fall into three broad categories 2-D vector graphics, imaging, and typography. The 2-D vector...

Overview of XDR Schemas

The example XML document data_dtd.xml used to demonstrate DTDs contains information about the modules in which a given class is articulated. The following listing shows the XDR schema that provides a full description of the class lt xml version 1.0 gt lt AttributeType name title dt type string gt lt AttributeType name company dt type string gt lt AttributeType name author dt type string gt lt AttributeType name total dt type int gt lt AttributeType name expandable dt type enumeration dt values...

Customizing Column Mapping

Each row in a DataTable object originates an XML subtree whose structure depends on the value assigned to the DataColumn object's ColumnMapping property. Table 9-3 lists the allowable column mappings. Table 9-3 The MappingType Enumeration Table 9-3 The MappingType Enumeration The column is mapped to an XML attribute on the row node. The column is mapped to an XML node element. The default setting. Table 9-3 The MappingType Enumeration Table 9-3 The MappingType Enumeration The column is not...

The Xml Node Base Class

When you work with XML DOM parsers, you mainly use the XmlDocument class. The XmlDocument class, however, derives from a base class, XmlNode, which provides all the core functions to navigate and create nodes. XmlNode is the abstract parent class of a handful of node-related classes that are available in the .NET Framework. Figure 5-2 shows the hierarchy of node classes. Figure 5-2 Graphical representation of the hierarchy of node classes and their relationships in the .NET Framework. Figure...

Reading Back Diff Grams

When reading a DiffGram, the DataSet object's ReadXml method first loads the data instance and creates all the necessary tables and rows. Each row is put in the added or modified state, as appropriate. All the diffgr id values are temporarily copied into an internal hash table defined as a property of the DataSet object. Each entry in the hash table references a DataRow object in the table being created. Next ReadXml processes the lt diffgr before gt section and reads the old values for the...

Writing Encoded Data

As mentioned in the section Methods of the XmlWriter Class, on page 141, the XML text writer object has two methods that write out XML data in a softly encrypted way using base64 and BinHex algorithms. The methods involved WriteBase64 and WriteBinHex have a rather straightforward interface. They simply take an array of bytes and write it out starting at a specified offset and for the specified number of bytes. As you saw in Chapter 2, XML reader classes have matching ReadBase64 and ReadBinHex...

The MarshalByRef Object Class

Inheriting from the MarshalByRefObject class is the key that enables user classes to be accessed across AppDomain boundaries in applications that support remoting. MarshalByRefObject is the base class for objects that communicate across AppDomains. Serializable classes that do not inherit from MarshalByRefObject, when instantiated from a remote assembly, are implicitly marshaled by value. Other classes are simply considered nonremotable. So if you want to write a remote component that uses the...

Schema Information in the Diff Gram

In general, the schema and the data should be kept in separate files and handled as truly independent entities. The schema and the data are tightly coupled, and if serialization is involved, you might want to consider putting schema information in-line in the data. In the .NET Framework, the WriteXml method does not provide the capability to include schema information along with the data. This is more of a design choice than an objective difficulty. An indirect confirmation comes from the XML...

Using an Annotated Mapping Schema

A more lightweight alternative to FOR XML EXPLICIT views is the annotated schema. SQL Server 2000 lets you create XML views by defining an XDR schema with special annotations that work like placeholders for selected data. Basically, instead of defining the schema using a new syntax and combining multiple virtual tables, you use a standard XML data definition language and map elements to columns using ad hoc annotations. The base version of SQL Server 2000 supports only XDR. If you want to use...

The NET XPath Navigation API

The XML DOM support for XPath expressions has a double goal. First, it smooths the transition from MSXML COM code to the .NET Framework. Second, it gives you a built-in and easy-to-use mechanism to search for nodes in a memory-mapped XML document. As mentioned, the core .NET API for processing XPath expressions is built into a tailor-made class named XPathNavigator. You access the navigator object either from the XmlDocument class or from the newest XPathDocument class. Figure 6-5 illustrates...

The Xml Serializer Classs Constructors

Table 11-1 lists all the public constructors available in the XmlSerializer class. This list does not include the default class constructor because it is declared as protected and, as such, is not intended to be used directly from the user's code. Table 11-1 Constructors of XmlSerializer Serializes objects of the specified type. Allows you to customize the default mapping between properties and XSD elements. Adds type information to elements. Useful if you don't have the source code for the...

SqIXml Command Methods

On instantiation, the SqlXmlCommand class creates an instance of the SQLXMLOLEDB provider. Interestingly, it does not make use of an explicit wrapper assembly but instead gets a COM object type using the static method GetTypeFromCLSID from the Type class. Next it instantiates the COM object using the Activator class. Note The Activator class contains methods to create types of objects locally or remotely, or obtain references to existing remote objects. Functionally equivalent to the new...

Namespace Node Navigation

As you might have noticed in Table 6-4, there are three types of move methods for element, attribute, and namespace nodes. Calling the wrong method on a node causes the whole operation to fail, and there is no change in the position of the navigator. Only MoveTo and MoveToRoot can be called on any node, irrespective of the type. In addition, attributes and namespaces also have ad hoc methods to return their values GetAttribute and GetNamespace. When you call either MoveToFirstNamespace or...

What the Xml Writer Class Cant Do

Although powerful and considerably feature-rich, an XML writer is not perfect it still leaves some margin for errors. To be more precise, the XmlWriter class certainly generates 100-percent well-formed code, but only if you pass on correct information. In particular, an XML writer does not check for invalid characters in element and attribute names. It also does not guarantee that any Unicode characters you use fit into the current encoding schema. As a consequence, any characters outside the...

The Xsl Transform Class

Now that we've seen how the XslTransform class implements the .NET Framework processor to transform XML data into arbitrary text using XSL style sheets, let's look more closely at its programming interface. As shown in the following code, XslTransform has only the default constructor. In addition, it is a sealed class, meaning that you can use it only as is and other classes can't inherit from it. The programming interface of the class is fairly simple and consists of just one public property...

The Soap RpcMethod Attribute

The RPC format is expressed by the SoapRpcMethod attribute and specifies that all parameters are encapsulated within a single XML element named after the Web service method, as shown in the following code. The RPC style does not support the Literal binding mode only the SOAP-encoded binding mode Encoded is accepted. POST salesreport SalesReportService.asmx HTTP 1.1 Content-Type text xml charset utf-8 Content-Length length SOAPAction xmlnet cs 0735618 011 GetSalesReportBarChart lt xml version...

The Diff Gram Viewer Application

To fully demonstrate the workings of XML DiffGrams, nothing is better than taking a DataSet object, entering some changes, and seeing how the corresponding DiffGram representation varies. For this purpose, I created the DiffGram Viewer Windows Forms application, shown in Figure 10-3. The application is available in this book's sample files. . rittrsj3 t FCIE iiplayiirtcciccTifi P Ctam rw'id iilniin N jnr lii Ihi D JL Sii P Ctam rw'id iilniin N jnr lii Ihi D JL Sii -.fliffsn 'iLffgiis ti ji...

The XPath Navigator Class

The programming interface of the navigator object is defined in the XPathNavigator abstract class. The XPathNavigator class represents a generic interface designed to act as a reader for any data that exposes its contents as XML. Functionally speaking, the XPathNavigator class is not much different from a pseudo-class that simply groups together all the XML DOM methods ChildNodes, SelectNodes, and SelectSingleNode to navigate the document contents. The big difference lies in the fact that...

Validating Against an XDR

An XML document can include its XDR schema as in-line code or simply link it as an external resource. The XmlValidatingReader class determines that a given document requires XDR validation if an x-schema namespace declaration is found. The following sample document, named data_xdr.xml, points to an XDR schema stored in an external resource the schema.xml file lt xml version 1.0 gt lt -- Sample XML document data_xdr.xml using XDR -- gt lt class title Applied XML Programming for .NET 5 expandable...

Adapting Data to Classes

Reading incoming XML data is itself a kind of deserialization. However, as we've seen, the XML deserializer can only re-create an instance of the type you pass when you create the XmlSerializer object. How can you comply with any difference in the schema of the target class and the incoming XML data That task is handled by the attribute overrides process for the XMLSerializer object, shown in Figure 11-2. Attribute overrides and or deserialization hooking Figure 11-2 Attribute overrides are...

Encoding Images as Bin Hex

Converting a Bitmap object to one of the commonly used image formats is a nonissue. You call the Save method on the Bitmap object, pick up one of the supported formats, and you're done. The real difficulty has to do with the planned use of this helper class. Remember, we designed this class for later use within a .NET Remoting server and a Web service. When Web services in particular are involved, having the helper class save the image to persistent storage just doesn't make sense. An...

Using MSXML in the NET Framework

As with other COM objects, you can import the MSXML type library within the boundaries of a .NET application. The layer of system code providing for COM importation in the .NET Framework is the COM Interop Services CIS . CIS provides access to existing COM components in a codeless and seamless way, without requiring modification of the original component. The CIS consists of two distinct parts one part makes COM components usable from within .NET applications, and the other part does the...