Calendar

<<  août 2008  >>
lumamejevesadi
28293031123
45678910
111214151617
181920222324
25262728293031
1234567

View posts in large calendar
Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008

(janvier 16, 2008 08:21)

We have seen in a previous post how to use an XmlDataSource and a GridView to display a formatted view of an XML document. We have seen also how we could use XSL to help in the formatting.

As you will go on and improve your formatting, you will probably want to add a common XSL file to include in several other XSL using either an <xsl:include or an <xsl:import tag. As soon as you will do that, you will receive an exception on your page "Resolving of external URIs was prohibited." And of course, as your included / impoorted XSL is in the same directory, you don't really see whay the access to this file is denied.

What's happening ? In fact all included / imported resources are considered as external resources, whatever their storage location. And after investigation in the code of the XmlDataSource, you can see it has not be written to be able to resolve external resources.

It's so impossible to use an XmlDataSource with an XSL using imported or included resources.

So how can we do ? Well... Just forget the XmlDataSource in that case. So how to achieve the same things in code ?

private void BindGridViewToXmlSource()
{
   XmlDocument dom = new XmlDocument();
   dom.LoadXml(GetTransformedXmlData("~/App_Data/YourXslFile.xsl", 
                                     "~/App_Data/YourXmlFile.xml"));
   gvPresentators.DataSource = dom.SelectNodes("YourXPathQuery");
   gvPresentators.DataBind();
}
 
/// <summary>
/// Return the content of the given XML path, 
/// transformed by the given Xsl file
/// </summary>
/// <param name="appRelXslPath">XSL file's app relative path</param>
/// <param name="appRelXmlPath">XML file's app relative path</param>
/// <returns></returns>
private string GetTransformedXmlData(string appRelXslPath, 
                                     string appRelativeXmlPath)
{
   XslCompiledTransform transform = new XslCompiledTransform();
   transform.Load(Server.MapPath(appRelXslPath));
   using ( MemoryStream stream = new MemoryStream() )
   {
      appRelativeXmlPath = Server.MapPath(appRelativeXmlPath);
      transform.Transform(appRelativeXmlPath, null, stream);
      stream.Position = 0;
      using ( StreamReader reader = new StreamReader(stream) )
         return reader.ReadToEnd();
   }
}

Billets liés

Ajouter un commentaire


 

  Country flag





Live preview

août 28. 2008 17:18

Powered by BlogEngine.NET 1.2.0.0 | Theme by Pierre-Emmanuel Dautreppe