Mark Wilson I am the creator of TopXML. I am available for international and local (Australia) contracts. I am a Solution Architect/Business Analyst. I have worked in IT in several countries (NZ, Australia, South Africa, UK) building and training teams for government and very large non-governmental organizations. I am ex-Microsoft Consulting Services. I wrote the first book on Microsoft XML published in 2000 called XML Programming with VB and ASP. Most recently I have been building tools for the SEO industry. Ask me for a 37 point SEO health-checkup for your website.
First posted :
03/24/2008
Times viewed :
636
XML Data Source
We will take the “programmer’s route” in this example and set the
properties with C# code instead of declaring the XML data source and the
transformation through attributes on the tag. The example page retrieves the
data with the help of the ProductManager class we already used in the previous
example. We assign the XML string returned by the ProductManager directly to
the web control’s DocumentSource property. If we stopped the example here, the
control would write the literal XML into the response page, but we wanted to
apply an XSLT stylesheet to turn the XML data into HTML. The simplest way to
point the control to the stylesheet is to set the TransformSource property to
the location of the stylesheet file we want to apply. Note: We have to set the
property to a local path reference, not a Url-type reference.
The example below shows the code for this page. To keep matters
simple we write the code in a code render block directly on the page instead of
setting up a code-behind file:
Xml1.DocumentContent = pm.GetProduct(
// Set the content source int.Parse( this.Request["id"] ) );
Xml1.TransformSource="product.xsl"; // Load the stylesheet
// from a file
%>
<asp:Xml id="Xml1" runat="server" />
</body>
</HTML>
Setting the DocumentContent property is not the only way to
specify the Xml data source. The Xml control class also allows referencing XML
data from XmlDocument objects or files through the properties Document and
DocumentSource respectively. Thus the
Xml Web Control allows a wide range of
possible data sources. Likewise we can specify the source of the stylesheet
either as a path on the file system or an XslTransform object.
TIP: Although the XML Web Control cannot reference XML data
from network sources, we can always read XML from any URL with the
XmlTextReader first. The we can assign the retrieved XML to the Document or the
DocumentContent property.
Specifying files as sources for the XML data and the XSLT
transformation has one great advantage – as long as they rarely change: The Xml
Web Control automatically caches XML data and XSLT transformations from files
in memory. The control reads in either file and puts the processed file content
in the cache. Therefore the expensive operations of parsing the XML file into
an XPathDocument and compiling the XSL stylesheet into an XslTransform object
only happen once. After the objects were put in the chance the Web Control
monitors the source files for changes and expires the cache items automatically.
Depending on the size of either file the time savings through caching the
processed files can be quite substantial. As an added benefit, all files in the
cache are accessible to all web controls within the web application reducing
processing even further.
Unfortunately the caching logic built
into the Xml Web Control only works with files. We have to implement our own
solution to cache data or stylesheets from other data sources. ASP.NET provides
great support for different caching strategies, as long as we can provide logic
to determine when the cached objects expire.
Caching data on the web tier is key to build data driven web
applications that scale linear to very high numbers of concurrent users. The
scalability of a web application goes down substantially if every page request
results in a database look up.
Databases in general (including SQL Server 2000) do not deal well with a
large number or concurrent database connections sustained over an extended period
of time. Caching the data on the web server reduces the number of database hits
and keeps web applications scalable even under heavily traffic.
With the plumbing provided by ASP.NET we
can cache XPathDocuments and XslTransform objects to achieve the same benefits
we get automatically from the web control by using files. We reduce the page
response time because we incur expensive operations like creating XPathDocument
and XslTransform objects only once. We also keep our applications scalable to
handle large numbers of concurrent users.
We cannot go into more detail here
because we are losing our focus on rendering XML. Be sure to check out the .NET
Framework documentation on caching features and the classes in the
System.Web.Caching namespace for more information.