Microsoft KB Archive/330586

= HOW TO Access protected document in Document Function of XSLT using Visual C# .Net =

Article ID: 330586

Article Last Modified on 2/19/2007

-

APPLIES TO


 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft .NET Framework 1.0
 * Microsoft .NET Framework 1.1

-



This article was previously published under Q330586





IN THIS TASK
SUMMARY
 * Create XMLdocument and XSLT
 * Create and run code

REFERENCES



SUMMARY
This step-by-step article demonstrates the process of instantiating and supplying a custom XmlUrlResolver to resolve document references to external XML resources that are located on authenticated Web sites or folders.

XmlTransform class uses the underlying XmlResolver class to read the files referenced in the XSL. You can specify the credentials to the XmlResolver. The XmlResolver class supports three methods for authenticating Web requests: Basic authentication, Digest authentication and Integrated Windows authentication.

back to the top

Create XMLdocument and XSLT
The following steps demonstrate the process to create sample XMLDocument and XSLT Document, which are used by sample code in Create and run code section.

  In a text editor such as Notepad, paste the following XML to create an XML Document and name it Books.xml. Save Books.xml in the Bin sub-folder of the project folder:    .NET Framework Programming 1   XML Programming</Title> 2</Author> </Book> </Books> </li> <li>Create a virtual directory in the web server named vdir. In the vdir Properties dialog, switch Directory Security tab, Click Edit to disable anonymous access. And enable the Basic authentication.</li> <li> In a text editor such as Notepad, paste the following XML to create an XML Document, and name it Authors.xml. Save Authors.xml in the vdir folder of web server: <?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?>   <Id>1</Id> <Name>John</Name> </Author>  <Id>2</Id> <Name>Peter</Name> </Author> </Authors> </li> <li> In a text editor such as Notepad, paste the following XSLT code to create an XSLT Document, and name it BookListing.xsl. Save BookListing.xsl in the Bin sub-folder of the project folder: <?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?> <xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;> <xsl:template match=&quot;Books&quot;> <HTML> <BODY> <H3>Book Listing</H3> </BODY> </HTML> </xsl:template> </xsl:stylesheet>

This style sheet is used to transform and to display the data in Books.xml in an HTML table. Notice that the document function is used to reference the XML document that contains Author data. This reference is used to display the names of the Authors whose Ids are referenced in the Book elements in Books.xml. </li></ol>

back to the top

Create and run code
<ol> <li>Start Visual Studio. NET, and create a new Visual C# Console Application project.</li> <li> Use the following code to replace the default code that is generated in Class1.cs (in the Visual C# project): using System; using System.Xml; using System.Xml.XPath; using System.Xml.Xsl; using System.Net;

namespace ConsoleApplication1 {   class Class1 {       [STAThread] static void Main(string[] args) {           XPathDocument xmldoc = new XPathDocument(&quot;Books.xml&quot;); XslTransform xslt = new XslTransform; xslt.Load(&quot;bookListing.xsl&quot;);

System.IO.FileStream fstream  = new System.IO.FileStream(&quot;bookListing.html&quot;, System.IO.FileMode.Create); XmlUrlResolver urlResolver = new XmlUrlResolver; // You can uncomment the following line to use Intergrated Authentication //     urlResolver.Credentials  = CredentialCache.DefaultCredentials ;

// You can uncommnet the following three lines to use the Basic or Digest Authentication // Before doing this, you need to ensure the User has sufficient permission to access the web site // And that IIS is configured correctly, or the accessing will fail //     CredentialCache cache = new CredentialCache; //     cache.Add(new Uri(&quot;http:// /vdir&quot;), &quot;Basic&quot;, new NetworkCredential(&quot;<UserName>&quot;, &quot;<Password>&quot;)); //     cache.Add(new Uri(&quot;http:// /vdir&quot;), &quot;Digest&quot;, new NetworkCredential(&quot;<UserName>&quot;, &quot;<Password>&quot;)); //     urlResolver.Credentials = cache;

xslt.XmlResolver = urlResolver;

xslt.Transform(xmldoc, null, fstream); fstream.Close;

Console.WriteLine(&quot;Done&quot;); }   } } </li> <li>Save the changes, and then run the project. The code runs successfully and returns control to the designer window. The output of the XSLT transformation is generated to an HTML file that is named BookListing.html in the Bin sub-folder of the project.</li> <li>Click the Show All Files and Refresh buttons in the Visual Studio. NET Solution Explorer window to display the sub-folders of the project.</li> <li>Expand the Bin sub-folder. You see the BookListing.html file that is generated by the XSLT transformation. Double-click BookListing.html to open it in Visual Studio .Net. Notice that the HTML table that is generated does not display the names of the authors for the books that are listed. This occurs because the XmlResolver that is used by the XSLT processor cannot read the Author.xml document. We didn't provide credentials to access the protected document.</li> <li>The commented code will give the XSLT processor a credential to access the web. The code use two authentication method. You can uncomment one of the code block.</li> <li>Save the change, and then run the project. Open the generated BookListing.html file to examine the output. You see that the HTML table lists the names of the Authors for each of the listed books, as you expect.</li></ol>

back to the top

<div class="references_section">