Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# xml.Load() locking file on disk causing errors

Tags:

I have a simple class XmlFileHelper as follows:

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 } 

The issue is I am getting the following error on Load:

System.IO.IOException: The process cannot access the file ''C:\CvarUAT\ReportWriterSettings.xml'' **because it is being used by another process** 

this occurs when this class is used to by two running instances of a component running in parallel both attempting to load the xml file above, this is legitimate behaviour and required by the application.

I only want to read in the xml off disk once and release any reference to the file on disk and use an in memory representation from that point forward.

I would have assumed Load operates in a readonly fashion and would have no need to lock the file, what is my best way to achieve the desired result and get around this issue?

Thanks

like image 955
m3ntat Avatar asked Nov 28 '09 14:11

m3ntat


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


1 Answers

You can do this

using (Stream s = File.OpenRead(xmlFilePath)) {     xmlDoc.Load(s); } 

instead of

xmlDoc.Load(xmlFilePath); 
like image 174
João Angelo Avatar answered Oct 12 '22 22:10

João Angelo