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:
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;
xmlDoc.LoadXml(sXMLSource);
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.Flush();
stream.Position = 0;
System.IO.StreamReader sr = new System.IO.StreamReader (stream);
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.