【发布时间】:2016-06-03 00:41:03
【问题描述】:
我有一个简单的类 XmlFileHelper 如下:
public class XmlFileHelper
{
#region Private Members
private XmlDocument xmlDoc = new XmlDocument();
private string xmlFilePath;
#endregion
#region Constructor
public XmlFileHelper(string xmlFilePath)
{
this.xmlFilePath = xmlFilePath;
xmlDoc.Load(xmlFilePath);
}
#endregion
#region Public Methods
public XmlNode SelectSingleNode(string xPathQuery)
{
return xmlDoc.SelectSingleNode(xPathQuery);
}
public string GetAttributeValueByName(XmlNode node, string attributeName)
{
return node.Attributes.GetNamedItem(attributeName).Value;
}
#endregion
#region Public Properties
public string XmlFilePath
{
get
{
return xmlFilePath;
}
}
#endregion
}
问题是我在加载时收到以下错误:
System.IO.IOException: The process cannot access the file ''C:\CvarUAT\ReportWriterSettings.xml'' **because it is being used by another process**
当这个类被并行运行的组件的两个运行实例用来加载上面的 xml 文件时,会发生这种情况,这是合法行为并且是应用程序所要求的。
我只想从磁盘读取 xml 一次,然后释放对磁盘上文件的任何引用,并从那时起使用内存中的表示。
我会假设 Load 以只读方式运行并且不需要锁定文件,我最好的方法是什么来达到预期的结果并解决这个问题?
谢谢
【问题讨论】: