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.

Using XPathnavigators ❘ 923<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

Using XsltArgumentList<br />

XsltArgumentList is a way that you can bind an object with methods to a namespace. Once this is done,<br />

you can invoke the methods during the transform. Here is an example:<br />

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

{<br />

//new XPathDocument<br />

XPathDocument doc = new XPathDocument("books.xml");<br />

//new XslTransform<br />

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

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

//new XmlTextWriter since we are creating a new xml document<br />

XmlWriter xw = new XmlTextWriter("argSample.xml", null);<br />

//create the XslArgumentList <strong>and</strong> new BookUtils object<br />

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

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

//this tells the argumentlist about BookUtils<br />

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

//new XPathNavigator<br />

XPathNavigator nav = doc.CreateNavigator();<br />

//do the transform<br />

trans.Transform(nav, argBook, xw);<br />

xw.Close();<br />

webBrowser1.Navigate(AppDomain.CurrentDomain.BaseDirectory + "argSample.xml");<br />

}<br />

code download XslSample01.sln<br />

The following is the code for the BooksUtil class. This is the class that will be called from the transform:<br />

class BookUtils<br />

{<br />

public BookUtils() { }<br />

public string ShowText()<br />

{<br />

return "This came from the ShowText method!";<br />

}<br />

}<br />

code snippet BookUtils.cs<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!