13.07.2015 Views

Applied XML Programming for Microsoft .NET.pdf - Csbdu.in

Applied XML Programming for Microsoft .NET.pdf - Csbdu.in

Applied XML Programming for Microsoft .NET.pdf - Csbdu.in

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

abstraction and explicitly <strong>in</strong>dicate the <strong>XML</strong> elements to create—nodes, attributes,comments, or process<strong>in</strong>g <strong>in</strong>structions. The writer works on a stream, dump<strong>in</strong>g content<strong>in</strong>crementally, one node after the next, without the random access capabilities of the<strong>XML</strong>DOM but also without its memory footpr<strong>in</strong>t.To grasp the importance of <strong>XML</strong> writers, consider that, <strong>in</strong> general, the only alternativeyou have <strong>for</strong> writ<strong>in</strong>g <strong>XML</strong> contents to any storage media consists of prepar<strong>in</strong>g the entireoutput as a str<strong>in</strong>g and then writ<strong>in</strong>g it off. In this case, the markup nature of <strong>XML</strong> is moreh<strong>in</strong>drance than real help, because you must yourself take care of the <strong>in</strong>tricacies ofquotation marks, attributes, <strong>in</strong>dentation, and end tags.In the .<strong>NET</strong> Framework, <strong>XML</strong> writers come to the rescue and let you write <strong>XML</strong>documents programmatically <strong>in</strong> much the same way you write them through texteditors. For example, you can specify whether you want a namespace prefix, thepadd<strong>in</strong>g character and the size of the <strong>in</strong>dentation, the quotation mark and the newl<strong>in</strong>echaracter, and even how you want white spaces to be treated. To create nodes, yousimply use ad hoc methods to write comments, attributes, and element nodes. Theoverall method of work<strong>in</strong>g is simple and extremely effective.The .<strong>NET</strong> Framework provides several types of writers that use heterogeneous outputdevices—str<strong>in</strong>gs, HTTP response, and HTML documents. You could also use an <strong>XML</strong>text writer to dump contents to a stream object or a new text file. In the latter two cases,you could also specify character encod<strong>in</strong>g. If the encod<strong>in</strong>g argument is null, theUnicode 8-bits-per-character schema (UTF-8) will be used.<strong>XML</strong> writers, and <strong>in</strong> particular the XmlTextWriter class, are used throughout the .<strong>NET</strong>Framework <strong>for</strong> creat<strong>in</strong>g any sort of <strong>XML</strong> output. We'll look at <strong>XML</strong> writers <strong>in</strong> detail <strong>in</strong>Chapter 4.The <strong>XML</strong> Document Object API <strong>in</strong> .<strong>NET</strong>As mentioned, along with <strong>XML</strong> readers and writers, the .<strong>NET</strong> Framework also providesclasses that load and edit <strong>XML</strong> documents accord<strong>in</strong>g to the W3C DOM Level 1 andLevel 2 Core. The key <strong>XML</strong>DOM class <strong>in</strong> the .<strong>NET</strong> Framework is XmlDocument—notmuch different from the DOMDocument class, which you might recognize from work<strong>in</strong>gwith MS<strong>XML</strong>.The <strong>XML</strong>DOM supplies an <strong>in</strong>-memory tree-based representation of <strong>XML</strong> documentsand supports both navigation and edit<strong>in</strong>g of the document. In addition, the <strong>XML</strong>DOMclasses can handle both XPath queries and XSLT.Tightly coupled with the XmlDocument class is the XmlDataDocument class. It extendsXmlDocument and focuses on <strong>XML</strong> storage and retrieval of structured tabular data. Inparticular, XmlDataDocument can import data from an ADO.<strong>NET</strong> DataSet object andexport regular <strong>XML</strong> contents to the DataSet relational <strong>for</strong>mat. Regular <strong>XML</strong> content is aset of nodes with exactly one level of subnodes, with each node hav<strong>in</strong>g the samenumber of children. The ultimate goal of this requirement is enabl<strong>in</strong>g the <strong>XML</strong> contentsto fit <strong>in</strong>to a relational table.The <strong>XML</strong>DOM representation of an <strong>XML</strong> document is fully editable. Attributes and textcan be randomly accessed, and nodes can be added and removed. You per<strong>for</strong>mupdates on a loaded <strong>XML</strong>DOM document by first creat<strong>in</strong>g a node object (the XmlNodeclass) and then b<strong>in</strong>d<strong>in</strong>g it to the exist<strong>in</strong>g tree. All <strong>in</strong> all, the underly<strong>in</strong>g writ<strong>in</strong>g pattern isclose to that of <strong>XML</strong> writers—you write nodes to the stream <strong>in</strong> one case, and you addnodes to the tree <strong>in</strong> the other. Of course, if you are us<strong>in</strong>g the <strong>XML</strong>DOM, bear <strong>in</strong> m<strong>in</strong>dthat all changes occur <strong>in</strong> memory and must be flushed to the storage medium prior toreturn. (The <strong>XML</strong>DOM API is described <strong>in</strong> detail <strong>in</strong> Chapter 5.)17

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!