【问题标题】:Load XML document in read-only access mode以只读访问模式加载 XML 文档
【发布时间】:2015-05-11 06:42:55
【问题描述】:

如何以只读模式加载 XML 文档?

我有一个在另一个进程中打开的 XML 文件,我想将它以只读方式加载到我的 C# 应用程序中。

XmlDocument.Load("file.xml") 显然会抛出这个错误:

进程无法访问文件,因为它正在被另一个文件使用 进程

所以我也尝试了流式阅读器:

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

但它也会引发同样的错误。 那么如何以只读模式访问我的 XML 文档呢?

更新

我也试过XPathDocumentFileStream("file.xml", FileMode.Open, FileAccess.Read,FileShare.Read)。但他们都没有解决问题。

【问题讨论】:

  • 其他进程做什么?如果该进程正在写入 XML 文档,那么就会有锁。
  • @Amit 它正在 Microsoft Word 中打开。但是当我试图打开文件时它并没有写进去
  • 尝试使用 new FileStream("file.xml", FileMode.Open, FileAccess.Read, FileShare.Read);
  • @Amit 仍然抛出同样的错误
  • 如果我在记事本中打开 xml 文档,上面的代码同时使用写字板。可能对代码有更深入的了解会有所帮助。

标签: c# xml file-access


【解决方案1】:

鉴于您说FileShare.Read 不起作用,看来其他进程已打开文件以供写入。

您可以尝试使用FileAccess.ReadFileShare.ReadWrite 打开它,在这种情况下,您需要处理如果其他进程确实写入文件时可能发生的任何错误。

如果这不起作用,很可能是另一个进程用FileShare.None 打开了它,在这种情况下,你无能为力。要检查这一点,请尝试使用记事本打开文件。

但是如果 FileShare.ReadWrite 在大多数情况下都有效,它是否仍有可能引发错误?

只有当另一个进程已经使用FileShare.None 打开了文件时,您才会收到错误消息。您已确认在 Microsoft Word 中打开时不是这种情况,因此您应该没问题。

【讨论】:

  • 终于成功了!!但是,如果FileShare.ReadWrite 在大多数情况下都有效,它仍然有可能引发错误吗?因为它总是显式地工作是非常重要的
【解决方案2】:

此类以只读模式显示读取的 xml 文件。

 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[]>();
            }
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多