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



The information in this article applies to:

  • Microsoft Visual C# .NET (2003)
  • Microsoft Visual C# .NET (2002)
  • Microsoft XML Classes (included with the .NET Framework 1.0)
  • Microsoft XML Classes (included with the .NET Framework 1.1)

This article was previously published under Q330586

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.
  1. 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:
       <?xml version="1.0" encoding="utf-8" ?>
       <Books>
       <Book>
       	<Title>.NET Framework Programming</Title>
       	<Author>1</Author>
       </Book>
       <Book>
       	<Title>XML Programming</Title>
       	<Author>2</Author>
       </Book>
       </Books>
  2. 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.
  3. 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="1.0" encoding="utf-8" ?> 
    <Authors>
    <Author>
    	<Id>1</Id>
    	<Name>John</Name>
    </Author>
    <Author>
    	<Id>2</Id>
    	<Name>Peter</Name>
    </Author>
    </Authors>
  4. 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="1.0" encoding="utf-8" ?> 
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     
     <xsl:template match="Books">
     <HTML>
     <BODY>
     <H3>Book Listing</H3>
     
     <TABLE BORDER="1">
    
     <TR>
    	<TH>Title</TH>
    	<TH>Author</TH>
     </TR>
    
    
     <xsl:for-each select="Book">
        <xsl:variable name="vId" select="Author"/>
    	<TR>
    		<TD>  
    			  <xsl:value-of select="Title"/>  
    		</TD>
    		<TD>  
    			  <xsl:value-of select="document('http://<server>/vdir/Authors.xml')/Authors/Author[Id=$vId]/Name"/>
    		</TD>
    	</TR>
      </xsl:for-each>
     
     </TABLE>
     </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.
back to the top

Create and run code

  1. Start Visual Studio. NET, and create a new Visual C# Console Application project.
  2. 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("Books.xml");
    			XslTransform xslt = new XslTransform();
    			xslt.Load("bookListing.xsl");
    
    			System.IO.FileStream  fstream  = new System.IO.FileStream("bookListing.html", 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("http://<server>/vdir"), "Basic", new NetworkCredential("<UserName>", "<Password>"));
    	    		//		cache.Add(new Uri("http://<server>/vdir"), "Digest", new NetworkCredential("<UserName>", "<Password>"));
    			//		urlResolver.Credentials = cache;
    
    			xslt.XmlResolver = urlResolver;
    
    			xslt.Transform(xmldoc, null, fstream);
    			fstream.Close();
    
    			Console.WriteLine("Done");
    		}
    	}
    }
    
  3. 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.
  4. Click the Show All Files and Refresh buttons in the Visual Studio. NET Solution Explorer window to display the sub-folders of the project.
  5. 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.
  6. 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.
  7. 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.
back to the top

Modification Type:MajorLast Reviewed:9/18/2003
Keywords:kbHOWTOmaster KB330586