15.02.2015 Views

C# 4 and .NET 4

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

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

924 ❘ ChaPTer 33 mAnipulAtinG xml<br />

The following is what the output of the transform looks like; the output has been formatted for easier<br />

viewing (argSample.xml):<br />

<br />

<br />

The Autobiography of Benjamin Franklin<br />

This came from the ShowText method!<br />

<br />

<br />

The Confidence Man<br />

This came from the ShowText method!<br />

<br />

<br />

The Gorgias<br />

This came from the ShowText method!<br />

<br />

<br />

The Great Cookie Caper<br />

This came from the ShowText method!<br />

<br />

<br />

A Really Great Book<br />

This came from the ShowText method!<br />

<br />

<br />

In this example, you define a new class, BookUtils. In this class, you have one rather useless method that<br />

returns the string This came from the ShowText method! In the button3_Click() event, you create<br />

the XPathDocument <strong>and</strong> XslTransform objects. In a previous example, you loaded the XML document<br />

<strong>and</strong> the transform document directly into the XslCompiledTransform object. This time, you will use the<br />

XPathNavigator to load the documents.<br />

Next, you need to write the following:<br />

XsltArgumentList argBook=new XsltArgumentList();<br />

BookUtils bu=new BookUtils();<br />

argBook.AddExtensionObject("urn:XslSample",bu);<br />

This is where you create the XsltArgumentList object. You create an instance of the BookUtils object,<br />

<strong>and</strong> when you call the AddExtensionObject() method, you pass in a namespace for your extension <strong>and</strong> the<br />

object that you want to be able to call methods from. When you make the Transform() call, you pass in<br />

the XsltArgumentList (argBook), along with the XPathNavigator <strong>and</strong> the XmlWriter object you made.<br />

The following is the booksarg.xsl document (based on books.xsl):<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!