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.

Using XPathnavigators ❘ 925<br />

<br />

<br />

<br />

<br />

<br />

The two important new lines are highlighted. First, you add the namespace that you created when you<br />

added the object to XsltArgumentList. Then, when you want to make the method call, you use st<strong>and</strong>ard<br />

XSLT namespace-prefixing syntax <strong>and</strong> make the method call.<br />

Another way you could have accomplished this is with XSLT scripting. You can include <strong>C#</strong>, Visual<br />

Basic, <strong>and</strong> JavaScript code in the stylesheet. The great thing about this is that unlike current non-.<strong>NET</strong><br />

implementations, the script is compiled at the XslTransform.Load() call; this way, you are executing<br />

already compiled scripts.<br />

Go ahead <strong>and</strong> modify the previous XSLT file in this way. First, you add the script to the stylesheet. You can<br />

see the following changes in booksscript.xsl:<br />

<br />

<br />

string ShowText()<br />

{<br />

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

}<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

Once again, the changes are highlighted. You set the scripting namespace, add the code (which was copied<br />

<strong>and</strong> pasted in from the Visual Studio .<strong>NET</strong> IDE), <strong>and</strong> make the call in the stylesheet. The output looks the<br />

same as that of the previous example.<br />

Debugging XslT<br />

Visual Studio 2010 has the capability to debug transforms. You can actually step through a transform line<br />

by line, inspect variables, access the call stack, <strong>and</strong> set breakpoints just like you were debugging <strong>C#</strong> source<br />

code. You can debug a transform in two ways: by just using the stylesheet <strong>and</strong> input XML file or by running<br />

the application that the transform belongs to.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!