An XML Namespace: System.Xml


The System.Xml namespace objects are designed to fully support your XML needs. Your needs may range from reading and writing XML to storing XML. In fact, your application's needs may even extend to querying XML or transforming XML. There are many feature-rich System.Xml namespace objects. In this section, I provide an overview of the System.Xml namespace and the objects contained therein.

Note  

I want to emphasize that this discussion is definitely an overview. A full discussion of the System.Xml namespace would likely fill an entire book. All the same, I do feel that this discussion will help shed some light on the topic and help you head in the right direction ” toward continued learning.

Choosing an XML Reader or XML Writer

Basically, you could take an either/or approach when you choose your System.Xml class for reading. For example, either you choose a derived class from the System.Xml.XmlReader base class for fast, forward-only, read-only, non-cached type reading. Or you can choose the Document Object Model (DOM) class for full-featured XML document reading and manipulation. Generally speaking, the DOM class is System.Xml.XmlDocument . Your choice for writing is much simpler. You will want to explore the derived class System.Xml.XmlTextWriter for your XML output needs. Figure 12-5 shows the basic hierarchy of the XmlReader and XmlTextWriter classes.

click to expand
Figure 12-5: A high-level view of the System.Xml.XmlReader and System.Xml.XmlWriter family of classes

If your reading/parsing needs point you in the direction of the XmlReaderclass, you would then hit another decision tree. For example, if you want the fastest reader and are willing to make a sacrifice on the feature side, then the XmlTextReader class might be your best bet. On the other hand, if you want to perform data/Schema validation, then the XmlValidatingReader class may serve your needs. The XmlNodeReader class is used for reading XmlNodes (i.e., reading portions of XML documents).

Tip  

Recall that I discussed the System.Data.SqlClient.SqlCommand class in Chapter 11 in the section "The System.Data.SqlClient Namespace." This particular DataProvider object exposes a method named ExecuteXmlReader. The return type of this method is System.Xml.XmlReader.

When you are ready to write XML with the XmlTextWriter class, you will generally aim to write well-formed XML documents. Fortunately, the XmlTextWriter class supports the W3C Recommendation (the Infoset Recommendation) that defines a standard of writing well- formed XML documents. Basically, the W3C Recommendation includes a definition for an XML Information Set (or Infoset). An Infoset is made up of any number of information items (such as a document, elements, attributes, and so on).

As you explore the System.Xml.XmlTextWriter class, you will encounter a collection of Write Xxxx methods (where Xxxx corresponds to the name of an Infoset information item). For example, the WriteStart Document , WriteStart Element , and WriteStart Attribute XmlTextWriter methods are used to write the "start" of the following information items: document, element, and attribute, respectively. If you look at the extensive list of WriteXxxx methods in this manner, the list may seem less exhaustive.

Cross-Reference  

You'll find the URL for the XML Information Set (Infoset) Recommendation at the end of this chapter in the "Web Sites" subsection of the "To Learn More" section.

The Document Object Model Class

One of the first things that you will come across as you dive into the Document Object Model (DOM) class (System.Xml.XmlDocument), is the realization that the class itself is derived from the System.Xml.XmlNode class (see Figure 12-6). The XmlDocument class simply extends the XmlNode class, providing an enhanced implementation of methods and so forth to read and manipulate the instantiated nodes. The DOM then is basically a hierarchical (parent/child) collection of nodes (or XmlNodes). You will often see a DOM presented in a tree view.

click to expand
Figure 12-6: The System.Xml.XmlNode class and the classes derived from it

As you see in Figure 12-6, several classes are derived from the XmlNode class. Knowing this, it should be easier for you to understand the discussions, presentations, and white papers that loosely describe the XmlDocument as being "made up of" the XmlNode , XmlElements , and XmlAttributes classes. Either directly or indirectly, they each inherit from the XmlNode class. In your coding, you can establish logical relationships between the "nodes" creating the parent/child tree structure.

The DOM class, XmlDocument, has the capability to read in XML files, streams, or XmlReader objects (as illustrated in Figure 12-7). Among the many public methods to learn about, you will want to start with the Load method. Using this method, you will be able to easily load XML data into an XmlDocument object.

click to expand
Figure 12-7: A general input and output view of the XmlDocument class

Extending the XmlDocument Class: XmlDataDocument

There is a class named System.Xml.XmlDataDocument that is derived from the XmlDocument class. The XmlDataDocument class provides yet another way to use the ADO.NET System.Data.Dataset to support your XML needs. The XmlDataDocument (or just data document ) offers public members that enable a quick "load" of your Dataset into the data document. You can load either relational data or XML data into an XmlDataDocument object .

Once you've done this, you can manipulate the XML "view" of your data using the exposed data document methods. Optionally, you can take advantage of the synchronizing feature. That's right ”you can easily synchronize your data document (XML view) to your Dataset (relational view). This synchronization allows a flexible handling approach when you need to update using one view or the other (in other words, using one technology/toolset or the other).

In an abbreviated fashion, that concludes this chapter's discussion of the XmlDocument class and its extended XmlDataDocument class. Obviously, many methods, properties, and so forth are exposed through each class, and all deserve further exploration. Please make a point to take on this extended exploration of the XmlDocument class (and the derived XmlDataDocument class). To finish off the chapter, you'll leave the System.Xml namespace to review several other namespaces that .NET offers for XML support.

.NET Replaces MSXML 4.0 (Almost)

Microsoft's XML parser, MSXML 4.0 (now known as Microsoft XML Core Services and previously described in Chapter 4 in the section "MSXML"), has historically provided much of the XML Document Object Model (DOM) support described in this section. The .NET XML managed objects largely overlap the functionality exposed in the COM-based MSXML 4.0 library.

Generally, you will want to use the managed objects offered in the various .NET XML namespaces. However, there are occasions when you should use the MSXML 4.0 product (e.g., for backward compatibility with legacy applications, to overcome the current XLST parsing performance issue, and so on). Use the .NET COM Interop feature (adding a COM reference to the Microsoft XML 4.0 MSXML4.dll, and available as free download from Microsoft's Web site) in your .NET applications to take advantage of the MSXML 4.0 library as appropriate.

For more information, please refer to the Microsoft Knowledge Base Article Q317728, which is available on the MSDN Web site.




COBOL and Visual Basic on .NET
COBOL and Visual Basic on .NET: A Guide for the Reformed Mainframe Programmer
ISBN: 1590590481
EAN: 2147483647
Year: 2003
Pages: 204

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net