Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load XML document in read-only access mode

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.

like image 505
Alex Jolig Avatar asked Aug 29 '26 10:08

Alex Jolig


2 Answers

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[]>();
            }
        }
like image 183
Dev-Systematix Avatar answered Sep 01 '26 01:09

Dev-Systematix


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.

like image 34
Joe Avatar answered Aug 31 '26 23:08

Joe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!