FREE BOOK

Chapter 7: Web Matrix and XML

Posted by Apress Free Book | XML .NET January 13, 2009
In this chapter Use the XML Support ,XML Notepad ,XMLEditGrid Control,XML File Page,XSL Transform Page,XML Schema Page and XML Data Display Application

Understanding XML Support in Web Matrix
 
The introduction to this chapter says a lot. XML is a text-based data storage technology that relies on tags to separate the various data elements. The formatting required for the tags is relatively straightforward and free form, but the specifi- cation includes precise rules you have to follow. For example, every opening tag requires a closing tag, or the document isn't well formed. Web Matrix helps you follow the rules required by the XML specifications, yet simplifies the task of reading and manipulating the data by making the XML classes of the .NET Framework available. (You'll find an overview of the XML classes at http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemXml.asp.)
 
This section of the chapter provides an overview of the XML support provided by the .NET Framework, and as a result, by Web Matrix. Microsoft groups the System.Xml namespace with other data-oriented namespaces such as System.OleDb. Essentially, the System.Xml namespace provides classes oriented toward the management and control of data. Many developers think that they'll only use XML for data transfer because of the placement of the System.Xml namespace in the hierarchy, and because of articles that they've read in the trade press and magazines. However, the System.Xml assembly has a lot more to offer than simple data import and export functionality.
 
XML has a basic structure that lends itself to distributed application development because every computer out there can interpret text. XML relies on a formal use of tags to delineate data elements and format them in a way that two machines can understand. Formatting requires the use of attributes that define the type, scope, and use of the data in question. An XML file usually contains a header that shows the version of XML in use. In some cases, XML also requires special constructs such as the CDATA section to ensure that the recipient interprets the XML-formatted code directly. It's also possible to add comments to an XML file to help document the content (although the use of comments is relatively uncommon except as a means of identifying the data source). Finally, you need some way to read and write XML to a data stream (be it a file or an Internet connection).
 
Now that you have some idea of what an XML namespace would have to include, let's look at some class specifics. The following list doesn't tell you about every class within System.Xml, but it does tell you about the classes you'll use most often. We'll use several of these classes in the examples in this chapter.
 
XmlAttribute and XmlAttributeCollection: Defines one or more data features such as type. Attributes normally appear as part of a database schema or within a Document Type Definition (DTD). Applications need attribute data to convert the text representation of information such as numbers to their locally supported type.
 
XmlCDataSection: Prevents the XmlReader from interpreting the associated text as tag input. An application could use a CDATA section for a number of purposes, including the transfer of HTML or other tag-based code. CDATA sections are also used with escaped or binary data.
 
XmlComment: Represents a comment within the XML document. Generally, vendors use comments to include data source information or standards adherence guidelines. However, comments can also document XML data or serve any other human readable text need the developer might have.
 
XmlDataDocument, XmlDocument, and XmlDocumentFragment: Contains all or part of an XML document. The XmlDataDocument class enables the developer to work with data found in a data set. Data stored using an XmlDataDocument can be retrieved, stored, and manipulated using the same features provided by a data set. Use the XmlDocument to represent the World Wide Web Consortium (W3C) Document Object Model (DOM) that relies on the typical tree representation of hierarchical data. An XmlDocumentFragment represents just a part of an XML document. Developers normally use an object of this class for data insertions into an existing tree structure.
 
XmlDeclaration: Contains the XML declaration node. The declaration node includes information such as the XML version, encoding level, readonly status, and namespace. An XML document usually contains a single declaration node, but it's possible to use multiple declaration nodes to provide multiple layers of data support.
 
XmlNode: Represents a single leaf of the XML data hierarchy. An XmlNode usually consists of a single tag pair with associated data (contained within an XmlText object). The XmlNode is the Root object for many other classes in the System.Xml namespace. For example, the XmlDocument and XmlDocumentFragment container classes are derived from XmlNode. At the lower end of the scale, XmlAttribute and XmlEntity are both leaf nodes based on XmlNode. In some cases, developers will use XmlNode directly to parse an XML document.
 
XmlNodeReader, XmlReader, XmlTextReader, and XmlValidatingReader: Performs a read of XML data from a document, stream, or other source. The XmlReader is the base class for all other readers. This reader provides fast, forward-only access to XML data of any type. The XmlNodeReader reads XML data from XmlNodes only-it doesn't work with schema or DTD data. The XmlTextReader doesn't perform as quickly as other readers, but it does work with DTD and schema data. This reader checks the document and nodes for well-formed XML, but doesn't perform any validation in the interest of speed. Use the XmlValidatingReader when the validity of the data is more important than application speed. This reader does perform DTD, XML-Data Reduced (XDR) schema, and XML Schema Definition (XSD) language schema validation. If either the XmlTextReader or XmlValidatingReader detect an error in the XML, both classes will raise an XmlException.
 
TIP Using the correct reader is the most important way to improve application reliability and performance.Using the slow XmlValidatingReader on simple node data ensures your application will perform poorly (much to the consternation of the user).On the other hand, using the XmlNodeReader on mission critical data could result in data loss and unreliable application operation. In fact, due to the need to resend missing or incorrectly resolved data, application performance could suffer as well.
 
XmlResolver and XmlUriResolver: Resolves external XML resources pointed to by a Uniform Resource Identifier (URI). For example, many common data types appear as definitions in external, standardsmaintained resources. In addition, external resources on your company's Web site, such as a DTD or schema, will also appear in this list. Most developers will use the default resolution capabilities provided by XmlUriResolver. However, you can also create your own resolver using the XmlResolver class as a basis.
 
XmlTextWriter and XmlWriter:
Performs a write of XML data to a data stream, file, or other output. The XmlWriter provides a means of manually controlling the output stream-a requirement in some cases. For example, the XmlWriter provides control over the start and stop of each data element and enables you to declare namespaces manually. However, the XmlTextWriter greatly simplifies the task of outputting data by performing some tasks automatically and making assumptions based on the current application environment.

Total Pages : 8 12345

comments