How to update .xsl file by multiple users at the same time?

222 views Asked by At

I am working on an application on an aplication in which multiple users update a xsl during their processing.This file is placed on a folder on server where web application is deployed.

Following code is used to update the xsl file.

XmlDocument doc = new XmlDocument();
doc.Load(ConfigurationManager.AppSettings["XSLPdfDimensions"]);
XmlNode root = doc.DocumentElement;
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("xsl", "http://www.w3.org/1999/XSL/Transform");

root.SelectSingleNode(marginTop, nsmgr).Attributes["select"].Value = Convert.ToString(top);
root.SelectSingleNode(marginBottom, nsmgr).Attributes["select"].Value = Convert.ToString(bottom);

doc.Save(ConfigurationManager.AppSettings["XSLPdfDimensions"]);

When users access this file concurrently following exception occurs

"The process cannot access the file 'C:\XSL\Dimensions.xsl'because it is being used by another process"

How to resolve this issue by minimum delay in functionality or there is some alternative approach for updating xsl file ?

2

There are 2 answers

0
Kevin Brown On BEST ANSWER

or there is some alternative approach for updating xsl file ?

Why not pass in the marginTop and marginBottom through code as parameters. That way you have one XSL, no modifications are required and all users get what they want.

Or modify the XML to pass in those values and rewrite the XSL to use those values from the XML. There should be no reason for user modification of the XSL in a well designed system. Either the XML or a parameter passed to the transformation can handle that variable information.

Since I see you are trying to pass PDF dimensions, you should look at the code here at http://www.cloudformatter.com/CSS2Pdf.APIDoc.Usage. If you look into that code you can get to the XSL behind that conversion. All of the appropriate page dimensions -- page size, all margins and many, many more things are all passed into that XSL from the XML (derived from the HTML div and/or specified in the Javascript and attached to the XML sent to the XSL).

There is no need for anyone to edit that XSL to get all kinds of things -- page sizes, margins, background images, page borders, page background colors, etc.

0
Alexey On

I would guess that you see this error when two or more users try to call doc.Save method. When one user starts writing to a file, operating system locks it preventing others from changing at same time. You might need to use it inside lock - them you'll make sure that only one thread would attempt to write to it at the same moment of time.

Another option is to redesign your application to avoid concurrently writing to a file system - it's a risky business exactly because of the concurrency issues.