3

How do I load an XML document in read-only mode?

I have an XML file which is opened in another process and I want to load it in my C# application as read-only.

XmlDocument.Load("file.xml") obviously throws this error:

Process cannot access a file because it is being used by another process

So I tried stream reader too:

FileStream fs = new FileStream("file.xml", FileMode.Open, FileAccess.Read);
xmldoc.Load(fs);

But it also throws the same error. So how can I access my XML Document in read-only mode?

Update

I tried XPathDocument and FileStream("file.xml", FileMode.Open, FileAccess.Read,FileShare.Read) as well. But neither of them solved the problem.

Ghasem
  • 14,455
  • 21
  • 138
  • 171

2 Answers2

2

This class Shows read xml file in read only mode.

 public List<string[]> GetRunningOrderOnTable(string tableNo, int shopid)
        {
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                string xmlFilePath = @"C:\inetpub\wwwroot\ShopAPI\XmlData\RunningTables.xml";
                //string xmlFilePath = HttpContext.Current.Server.MapPath("~/XmlData/RunningTables.xml");
      // Option 1
                //                FileStream xmlFile = new FileStream(xmlFilePath, FileMode.Open,
                //FileAccess.Read, FileShare.Read);
                //                xmlDoc.Load(xmlFile);
      // Option 2
                using (Stream s = File.OpenRead(xmlFilePath))
                {
                    xmlDoc.Load(s);
                }
                //xmlDoc.Load(xmlFilePath);
                List<string[]> st = new List<string[]>();
                XmlNodeList userNodes = xmlDoc.SelectNodes("//Tables/Table");
                if (userNodes != null)
                {
                    foreach (XmlNode userNode in userNodes)
                    {
                        string tblNo = userNode.Attributes["No"].Value;
                        string sid = userNode.Attributes["ShopID"].Value;
                        if (tblNo == tableNo && sid == shopid.ToString())
                        {
                            string[] str = new string[5];
                            str[0] = userNode.Attributes["No"].Value;
                            str[1] = userNode.InnerText; // OrderNumber
                            str[2] = userNode.Attributes["OrderID"].Value;
                            str[3] = userNode.Attributes["OrderedOn"].Value;
                            str[4] = userNode.Attributes["TotalAmount"].Value;
                            st.Add(str);
                        }
                    }
                }
                else return new List<string[]>();
                return st;
            }
            catch (Exception ex)
            {

                CustomLogging.Log("RunningTables.xml GetRunningOrderOnTable Error " + ex.StackTrace, LoggingType.XMLRead);
                return new List<string[]>();
            }
        }
Dev-Systematix
  • 439
  • 6
  • 26
1

Given you've said that FileShare.Read doesn't work, it would appear that the other process has the file open for writing.

You could try opening it with FileAccess.Read and FileShare.ReadWrite, in which case you'll need to handle any errors that may occur if the other process does actually write to the file.

If that doesn't work, it's likely that the other process has it opened with FileShare.None, in which case there's nothing you can do about it. To check this, try opening the file with, say, Notepad.

But is it still possible for FileShare.ReadWrite to throws error if it works in most cases?

You will only get an error if another process has already opened the file using FileShare.None. You've confirmed that this isn't the case when it's open in Microsoft Word, so you should be OK.

Joe
  • 122,218
  • 32
  • 205
  • 338
  • Finally something worked!! But is it still possible for `FileShare.ReadWrite` to throws error if it works in most cases? Cause it's really important for it to always explicitly works – Ghasem May 11 '15 at 08:20