Home arrow XML arrow Page 6 - Designing Your Own Reporting Service: A Web Service to Convert XML to HTML Using XSL

Designing Your Own Reporting Service: A Web Service to Convert XML to HTML Using XSL

This article guides you in developing a web service which converts a given XML string to HTML based on XSL file available at server side.

Author Info:
By: Jagadish Chaterjee
Rating: 4 stars4 stars4 stars4 stars4 stars / 11
December 19, 2005
  1. · Designing Your Own Reporting Service: A Web Service to Convert XML to HTML Using XSL
  2. · Developing the XML Web Service
  3. · Developing the XML Web Service continued
  4. · Defining the XSLT
  5. · Executing and testing the XML Web Service
  6. · Understanding the web service
  7. · Improvements to the solution

print this article

Designing Your Own Reporting Service: A Web Service to Convert XML to HTML Using XSL - Understanding the web service
(Page 6 of 7 )

As you have successfully executed and tested the web service, let us go into the details of web service.  For the sake of clarity, I shall explain it to you part by part.

Let us first consider the following code fragment:

                  // Create the XslTransform.
                  XslTransform xslt = new XslTransform();
                  XmlUrlResolver resolver = new XmlUrlResolver();
                  resolver.Credentials = System.Net.CredentialCache.DefaultCredentials;

The above code fragment starts by creating an object “xslt” based on the class “XslTransform”.  The class “XslTransform” is mainly used to load XSL files and transform the XML accordingly. “XmlUrlResolver” is used to resolve external XML resources such as entities, document type definitions (DTDs) or schemas. It is also used to process include and import elements found in the Extensible StyleSheet Language (XSL) style sheets or XML Schema definition language (XSD) schemas.

If the virtual directory does not require authentication, this property does not need to be set. Otherwise, the credentials of the user must be supplied. Different credentials can be associated with different URIs and added to a credential cache. The credentials can then be used to check authentication for different URIs, regardless of the original source of the XML.  In the above case, it simply uses the default credentials.

Further proceeding we have the following two:

// Load the stylesheet.
                  string sXSLLocation = GetXSLLocation();
                  xslt.Load( sXSLLocation );

I think you can understand the above two very easily.  I simply get the XSL file location from the “web.config” file and load it to transform.  Next we have the following:

                  // Load the XML string.
                  XmlDataDocument xmlDoc = new XmlDataDocument();
                  xmlDoc.XmlResolver = resolver;

The above statements create a new “XmlDataDocument” object and load the XML string as “native XML” instead of “string based XML”.  This would help us to transform the XML very easily.

                  Stream stream = new MemoryStream();
                  xslt.Transform(xmlDoc, null, stream, resolver);
                  stream.Position = 0;
                  System.IO.StreamReader sr = new System.IO.StreamReader
                  return sr.ReadToEnd();

The above mainly creates a “MemoryStream”, to hold the entire transformation.  The second statement transforms the XML to XSL based on the credentials and  writes it into the memory stream.  Finally, we use a “StreamReader” to read the entire information available in memory stream.

blog comments powered by Disqus

- Open XML Finally Supported by MS Office
- XML Features Added to Two Systems
- Using Regions with XSL Formatting Objects
- Using XSL Formatting Objects
- More Schematron Features
- Schematron Patterns and Validation
- Using Schematron
- Datatypes and More in RELAX NG
- Providing Options in RELAX NG
- An Introduction to RELAX NG
- Path, Predicates, and XQuery
- Using Predicates with XQuery
- Navigating Input Documents Using Paths
- XML Basics
- Introduction to XPath

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials