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.

The XslTrans<strong>for</strong>m ClassNow that we've seen how the XslTrans<strong>for</strong>m class implements the .<strong>NET</strong> Frameworkprocessor to trans<strong>for</strong>m <strong>XML</strong> data <strong>in</strong>to arbitrary text us<strong>in</strong>g XSL style sheets, let's lookmore closely at its programm<strong>in</strong>g <strong>in</strong>terface.As shown <strong>in</strong> the follow<strong>in</strong>g code, XslTrans<strong>for</strong>m has only the default constructor. Inaddition, it is a sealed class, mean<strong>in</strong>g that you can use it only as is and other classescan't <strong>in</strong>herit from it.public sealed class XslTrans<strong>for</strong>m{}⋮The programm<strong>in</strong>g <strong>in</strong>terface of the class is fairly simple and consists of just one publicproperty and a couple of methods.Properties of the XslTrans<strong>for</strong>m ClassThe only property that the XslTrans<strong>for</strong>m class exposes is XmlResolver, which handlesan <strong>in</strong>stance of the XmlResolver class. Interest<strong>in</strong>gly, the XmlResolver property is writeonly—thatis, you can set it, but you can't check the currently set resolver object.As we've seen <strong>in</strong> previous chapters, the XmlResolver object is used to resolve externalreferences found <strong>in</strong> the documents be<strong>in</strong>g processed. In this context, the XmlResolverproperty is used only dur<strong>in</strong>g the trans<strong>for</strong>mation process. It is not used, <strong>for</strong> example, toresolve external resources dur<strong>in</strong>g load operations.If you don't create a custom resolver object, an <strong>in</strong>stance of the XmlUrlResolver class isused.Methods of the XslTrans<strong>for</strong>m ClassTheXslTrans<strong>for</strong>m class supplies two methods specific to its activity—the Load andTrans<strong>for</strong>m methods mentioned earlier. The Load and Trans<strong>for</strong>m methods are described<strong>in</strong> more detail <strong>in</strong> Table 7-5.Table 7-5: Methods of the XSLT ProcessorMethodDescriptionLoadLoads the specified XSLT style sheet document from a numberof possible sources, <strong>in</strong>clud<strong>in</strong>g remote URLs and <strong>XML</strong> readers.The method has several overloads, <strong>in</strong>clud<strong>in</strong>g overloads that letyou specify a custom XmlResolver object to load any stylesheets referenced through xsl:import and xsl:<strong>in</strong>cludestatements.Trans<strong>for</strong>m Trans<strong>for</strong>ms the specified <strong>XML</strong> data us<strong>in</strong>g the loaded XSLTstyle sheet and writes the results to a given stream. Some ofthe method's overloads let you specify an argument list as<strong>in</strong>put to the trans<strong>for</strong>mation.The follow<strong>in</strong>g code snippet shows how to use an XmlResolver object with credentials toaccess a remote XSLT style sheet:XmlUrlResolver resolver = new XmlUrlResolver();NetworkCredential cred = new NetworkCredential(uid, pswd,doma<strong>in</strong>);resolver.Credentials = cred;XslTrans<strong>for</strong>m xslt = new XslTrans<strong>for</strong>m();252

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

Saved successfully!

Ooh no, something went wrong!