15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

922 ❘ ChaPTer 33 mAnipulAtinG xml<br />

Nodes can be inserted before or after a selected node. The nodes can also be changed, <strong>and</strong> they can be<br />

deleted. If you have changes that have to be done to large numbers of nodes, using the XPathNavigator<br />

created from an XmlDocument may be your best choice.<br />

The system.Xml.Xsl namespace<br />

The System.Xml.Xsl namespace contains the classes that the .<strong>NET</strong> Framework uses to support XSL<br />

transforms. The contents of this namespace are available to any store whose classes implement the<br />

IXPathNavigable interface. In the .<strong>NET</strong> Framework, that would currently include XmlDocument,<br />

XmlDataDocument, <strong>and</strong> XPathDocument. Again, just as with XPath, use the store that makes the most<br />

sense. If you plan to create a custom store, such as one using the file system <strong>and</strong> you want to be able to do<br />

transforms, be sure to implement the IXPathNavigable interface in your class.<br />

XSLT is based on a streaming pull model. Because of this, you can chain several transforms together. You<br />

could even apply a custom reader between transforms if needed. This allows a great deal of flexibility in design.<br />

Transforming XMl<br />

The first example you will look at takes the books.xml document <strong>and</strong> transforms it into a simple HTML<br />

document for display, using the XSLT file books.xsl. (This code is in the XSLSample01 folder.) You will<br />

need to add the following using statements:<br />

using System.IO;<br />

using System.Xml.Xsl;<br />

using System.Xml.XPath;<br />

The following is the code to perform the transform:<br />

private void button1_Click(object sender, EventArgs e)<br />

{<br />

XslCompiledTransform trans = new XslCompiledTransform();<br />

trans.Load("books.xsl");<br />

trans.Transform("books.xml", "out.html");<br />

webBrowser1.Navigate(AppDomain.CurrentDomain.BaseDirectory + "out.html");<br />

}<br />

code download XslSample01.sln<br />

A transform doesn’t get any simpler than this. First, a new XmlCompiledTransform object is created. It<br />

loads the books.xsl transform document <strong>and</strong> then performs the transform. In this example, a string with<br />

the filename is used as the input. The output is out.html. This file is then loaded into the web browser<br />

control used on the form. Instead of using the filename books.xml as the input document, you can use an<br />

IXPathNavigable-based object. This would be any object that can create an XPathNavigator.<br />

After the XmlCompiledTransform object is created <strong>and</strong> the stylesheet is loaded, the transform is performed.<br />

The Transform method can take just about any combination of IXPathNavigable objects, Streams,<br />

TextWriters, XmlWriters, <strong>and</strong> URIs as parameters. This allows a great deal of flexibility on transform<br />

flow. You can pass the output of one transform in as the input to the next transform.<br />

XsltArgumentLists <strong>and</strong> XmlResolver objects are also included in the parameter options. We look at the<br />

XsltArgumentList object in the next section. XmlResolver-based objects are used to resolve items that are<br />

external to the current document. This could be things such as schemas, credentials, or, of course, stylesheets.<br />

The books.xsl document is a fairly straightforward stylesheet. The document looks like this:<br />

<br />

<br />

<br />

<br />

Price List<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!