如何同时由多个用户更新 .xsl 文件?

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

我正在开发一个应用程序,其中多个用户在 processing.This 文件放置在部署了 Web 应用程序的服务器上的文件夹中时更新 xsl。

以下代码用于更新 xsl 文件。

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"]);

当用户并发访问此文件时出现以下异常

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

如何通过最小的功能延迟来解决这个问题,或者有一些更新 xsl 文件的替代方法?

我猜您会在两个或更多用户尝试调用 doc.Save 方法时看到此错误。当一个用户开始写入文件时,操作系统会锁定它以防止其他人同时更改。您可能需要在 lock 内部使用它 - 您将确保只有一个线程会在同一时刻尝试写入它。

另一种选择是重新设计您的应用程序以避免同时写入文件系统 - 由于并发问题,这是一项有风险的业务。

or there is some alternative approach for updating xsl file ?

为什么不通过代码传入marginTop和marginBottom作为参数。这样您就有了一个 XSL,不需要任何修改,所有用户都能得到他们想要的。

或修改 XML 以传递这些值并重写 XSL 以使用 XML 中的那些值。在一个设计良好的系统中,用户应该没有理由修改 XSL。 XML 或传递给转换的参数都可以处理该变量信息。

由于我看到您正在尝试传递 PDF 尺寸,因此您应该查看 http://www.cloudformatter.com/CSS2Pdf.APIDoc.Usage 中的代码。如果您查看该代码,您可以了解该转换背后的 XSL。所有适当的页面尺寸——页面大小、所有页边距和许多其他内容都从 XML(源自 HTML div and/or 在 Javascript 中指定并附加到发送到 XSL 的 XML)。

任何人都不需要编辑该 XSL 来获得各种东西 -- 页面大小、页边距、背景图像、页面边框、页面背景颜色等。