Scott comments here on a utility XmlUrlResolver he wrote to load schemas embedded as resources in .NET assemblies that contain includes between them. This is a powerful feature, and it's nice to see how the XML stack was open to extension in ways like this.

FWIW, I did something like this a few years back (2002, actually) to load XSLTs embedded as resources for the original NUnitReport task for NAnt. Here's the code I wrote back then, which is pretty similar to Scott's code (except that I used an explicit URI schema):


///

/// Loads the XSLT Transform

///

///

/// This method will load the file specified

/// through the the xslfile attribute, or

/// the default transformation included

/// as a managed resource.

///

/// The Transformation to use

private XslTransform LoadTransform()

{

   XslTransform xslt = new XslTransform();

   if ( XslFile != null )

   {

      xslt.Load(XslFile);

   } else

   {

      XmlResolver resolver = new LocalResXmlResolver();

      Stream stream =

         (Stream)resolver.GetEntity(new Uri(XSL_DEF_FILE), null, null);

      XmlTextReader reader = new XmlTextReader(XSL_DEF_FILE, stream);

      xslt.Load(reader, resolver);

   }

   return xslt;

}

 

 

///

/// Custom XmlResolver used to load the

/// XSLT files out of this assembly resources.

///

internal class LocalResXmlResolver : XmlUrlResolver

{

   const string SCHEME_MRES = "mres";

 

   ///

   /// Loads the XSLT file

   ///

   ///

   ///

   ///

   ///

   public override object GetEntity(Uri absoluteUri, string role, Type objToReturn)

   {

      if ( absoluteUri.Scheme != SCHEME_MRES )

      {

         // we don't know how to handle this URI scheme....

         return base.GetEntity(absoluteUri, role, objToReturn);

      }

      Assembly thisAssm = Assembly.GetExecutingAssembly();

      string filename = absoluteUri.Segments[absoluteUri.Segments.Length - 1];

      return thisAssm.GetManifestResourceStream(filename);

   }

 


Tomas Restrepo

Software developer located in Colombia.