Microsoft KB Archive/330587

= HOW TO: Access Protected Document in Document Function of XSLT Using Visual Basic .NET =

Article ID: 330587

Article Last Modified on 9/4/2003

-

APPLIES TO


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

-



This article was previously published under Q330587





IN THIS TASK

 * SUMMARY
 * Create XML Document and XSLT Document
 * Create and Run the Code
 * REFERENCES



SUMMARY
This step-by-step article describes how to create an instance of and supply a custom XmlUrlResolver class to resolve document function references to external XML resources that are located on authenticated Web sites or folders.

The XmlTransform class uses the underlying XmlResolver class to read the files that are referenced in the XSL. You can specify the credentials to the XmlResolver. The XmlResolver class supports the following three methods to authenticate Web requests:
 * Basic authentication
 * Digest authentication
 * Windows authentication

back to the top

Create XML Document and XSLT Document
To create a sample XML Document and XSLT Document, follow these steps:

Note XML Document and XSLT Document are used by the sample code in the &quot;Create and Run the Code&quot; section of this article.   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 subfolder of the project folder:    .NET Framework Programming 1   XML Programming</Title> 2</Author> </Book> </Books> </li> <li>Create a virtual directory on the Web server, and then name it vdir .</li> <li>Right-click vdir, and then click Properties. On the Directory Security tab, click Edit. Clear the Anonymous access check box, and then select the Basic authentication check box.</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 subfolder 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 the Code
<ol> <li>In Visual Studio. NET, create a new Visual Basic .NET Console Application project.</li> <li> Use the following code to replace the default code that is generated in module1.vb (in the Visual Basic .NET project): Imports System Imports System.Xml.XPath Imports System.Xml.Xsl Imports System.Net

Module Module1

Sub Main Dim xmldoc As New XPathDocument(&quot;Books.xml&quot;)

Dim xslt As New XslTransform xslt.Load(&quot;bookListing.xsl&quot;)

Dim fstream As New System.IO.FileStream(&quot;bookListing.html&quot;, IO.FileMode.Create)

Dim urlResolver As New Xml.XmlUrlResolver

' You can uncomment the following line to use Intergrated Authentication 'urlResolver.Credentials = CredentialCache.DefaultCredentials

'You can uncomment the following three lines to use the Basic or Digest Authentication 'Before you do this, you must make sure that the User has sufficient permission to access the Web site 'And that IIS is configured correctly, or the accessing will fail

'Dim cache As New CredentialCache 'cache.Add(New Uri(&quot;http://server/vdir&quot;), &quot;Basic&quot;, New NetworkCredential(&quot;UserName&quot;, &quot;Password&quot;)) 'cache.Add(New Uri(&quot;http://server/vdir&quot;), &quot;Digest&quot;, New NetworkCredential(&quot;UserName&quot;, &quot;Password&quot;)) 'urlResolver.Credentials = cache

xslt.XmlResolver = urlResolver xslt.Transform(xmldoc, Nothing, fstream) fstream.Close

Console.WriteLine(&quot;Done&quot;)

End Sub

End Module </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 subfolder of the project.</li> <li>In Solution Explorer, click the Show All Files and Refresh buttons to display the subfolders of the project.</li> <li>Expand the Bin subfolder. 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 behavior occurs because the XmlResolver that is used by the XSLT processor cannot read the Author.xml document. Credentials were not provided to access the protected document.</li> <li>The commented code gives the XSLT processor a credential to access the Web. The codes use two authentication methods. Uncomment one of the code blocks.</li> <li>Save the change, and then run the project. Open the BookListing.html file that is generated 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">