I have a simple class XmlFileHelper as follows:
public class XmlFileHelper
{
#region Private Members
private XmlDocument xmlDoc = new XmlDocument();
it depends on what you need from the file,
If you need it to be threasdsafe you would need to impliment a mutex to lock the loading between instance,
If you dont really need thread safe loading (i.e. the file never changes) you could load it via a filestream then load the XmlDocument from the stream
FileStream xmlFile = new FileStream(xmlFilePath, FileMode.Open,
FileAccess.Read, FileShare.Read);
xmlDoc.Load(xmlFile);
try:
xml.Load(
new StreamReader(
new FileStream(
path,
FileMode.Open,
FileAccess.Read,
FileShare.Read)
)
);
You can do this
using (Stream s = File.OpenRead(xmlFilePath))
{
xmlDoc.Load(s);
}
instead of
xmlDoc.Load(xmlFilePath);
If the file isn't too big to read into memory all at once:
xml.Load(new MemoryStream(File.ReadAllBytes(path)));