[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 199
  • Last Modified:

Read XSL file from Web Service

Hi Experts,

I have a C# application that does a lot reporting. These reports are web based and formatted using XSL files located on the local drive. A config file with key-value settings is used to store the path of the location of theses 3 files.
For example:
Key="FileOne" Value="H:\formatting\stylesheet\file1.xsl"
Key="FileTwo" Value="H:\formatting\stylesheet\file3.xsl"
Key="FileThree" Value="H:\formatting\stylesheet\file3.xsl"

Everything is working fine as it is. However theses 3 XSL files now need to be shared corporately. And we came up with the use of a web service.

I know basic how to create a web service using Visual Studio 05 - However I need to know if it possible to "dump" these files into a web service  and have their configuration value point to the URL - if it's possible - then how would I implement it?

  • 2
1 Solution
It is surely possible to do this via a web service. However, I think this would be overkill because (i) it's only a one-way communication from server to client and (ii) the information on the server is static.
Just put the XSL files on your intranet server and use the WebRequest class to fetch it. In the config file you need the URL of the XSL files instead of local file paths and names.
The following is a code snippet from the online help of Visual Studio 05 illustrating the straight forward use of the WebRequest class:

            ' Create a request for the URL.        
            Dim request As WebRequest = WebRequest.Create("http://www.contoso.com/default.html")
            ' If required by the server, set the credentials.
            request.Credentials = CredentialCache.DefaultCredentials
            ' Get the response.
            Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
            ' Display the status.
            ' Get the stream containing content returned by the server.
            Dim dataStream As Stream = response.GetResponseStream()
            ' Open the stream using a StreamReader for easy access.
            Dim reader As New StreamReader(dataStream)
            ' Read the content.
            Dim responseFromServer As String = reader.ReadToEnd()
            ' Display the content.
            ' Cleanup the streams and the response.
yaney00074Author Commented:
thanx for the feedback

However it's little too complicaed to implement. I need a quick fix.
Let's drop the web service thing if there's a potential problem.

How would I use it with a website?
As I see it, the wesite is approach *is* the quickest and most uncomplicated fix. Web service brings much more implementation overhead.

> However it's little too complicaed to implement.
I don't quite understand you. The above code snippet contains 11 lines of code. This is too complicated? What did you expect?
A web service involves a running script/asp.net at server side handling the request. Although VS05 has great support for web services this is more complicated than inserting 11 lines of code :)
And be aware, using a web service you will get a string or stream as return output. Just the same as with WebRequest.

> How would I use it with a website?
Well, that depends very much on how you used the local file up to now. Do you open a StreamReader, use a string, or just feed the filename to a black box?
Do you use XslCompiledTransform or XmlReader?

What to do:
1) Put the XSL files on your web server, e.g. at http://my.company.com/formatting/stylesheet/file1.xsl
2) Fetch it from the web
      WebRequest req = WebRequest.Create("http://my.company.com/formatting/stylesheet/file1.xsl");
      HttpWebResponse webRes = (HttpWebResponse) req.GetResponse();
3) Initialize a stream reader with the content of file1.xsl
      StreamReader resStream = new StreamReader(webRes.GetResponseStream());
4) Use the stream reader directly,
    or read it to a string for further use
      string resString = resStream.ReadToEnd();
    or use the stream to create a XmlReader
      XmlReader xmlReader = XmlReader.Create(resStream)
    or load the XSL definition into a XslCompiledTransform object
    or write it to a temporary file (if you require a local file for further processing)
      StreamWriter tempStream = new StreamWriter("H:\temp\rand_id.xsl");
5) close resources

If you are using a XslCompiledTransform object, you might get away without any of the above stuff. Just look at the VS05 help for XslCompiledTransform.Load(String). As parameter you can specify an URI to the XSL file. Thus,
   XslCompiledTransform xslt = new XslCompiledTransform();
should also work. I have never tried it, though.

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now