c # xml.Load () bloquea el archivo en el disco causando errores
Tengo una clase simple XmlFileHelper como sigue:
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
}
El problema es que estoy recibiendo el siguiente error en la carga:
System.IO.IOException: The process cannot access the file ''C:\CvarUAT\ReportWriterSettings.xml'' **because it is being used by another process**
esto ocurre cuando dos instancias en ejecución de un componente que se ejecutan en paralelo utilizan esta clase, ambos intentan cargar el archivo xml anterior, esto es un comportamiento legítimo y es requerido por la aplicación.
Solo quiero leer el xml del disco una vez y lanzar cualquier referencia al archivo en el disco y usar una representación en memoria desde ese momento en adelante.
Habría asumido que Load funciona de una manera de solo lectura y no tendría necesidad de bloquear el archivo. ¿Cuál es mi mejor manera de lograr el resultado deseado y solucionar este problema?
Gracias