以只读访问模式加载 XML 个文档
Load XML document in read-only access mode
如何以只读模式加载 XML 文档?
我有一个在另一个进程中打开的 XML 文件,我想将它以只读方式加载到我的 C# 应用程序中。
XmlDocument.Load("file.xml")
显然会抛出这个错误:
Process cannot access a file because it is being used by another
process
所以我也尝试了流 reader:
FileStream fs = new FileStream("file.xml", FileMode.Open, FileAccess.Read);
xmldoc.Load(fs);
但它也抛出同样的错误。
那么如何以只读模式访问我的 XML 文档?
更新
我也尝试了 XPathDocument
和 FileStream("file.xml", FileMode.Open, FileAccess.Read,FileShare.Read)
。但是都没有解决问题
鉴于您已经说过 FileShare.Read
不起作用,其他进程似乎已打开文件进行写入。
您可以尝试使用 FileAccess.Read
和 FileShare.ReadWrite
打开它,在这种情况下,如果其他进程确实写入文件,您将需要处理可能发生的任何错误。
如果这不起作用,则可能是其他进程使用 FileShare.None
打开了它,在这种情况下,您无能为力。要检查这一点,请尝试使用记事本打开文件。
But is it still possible for FileShare.ReadWrite to throws error if it works in most cases?
如果另一个进程已经使用 FileShare.None
打开文件,您只会收到错误消息。您已确认在 Microsoft Word 中打开时不会出现这种情况,因此您应该没问题。
此 class 以只读模式显示已读 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[]>();
}
}
如何以只读模式加载 XML 文档?
我有一个在另一个进程中打开的 XML 文件,我想将它以只读方式加载到我的 C# 应用程序中。
XmlDocument.Load("file.xml")
显然会抛出这个错误:
Process cannot access a file because it is being used by another process
所以我也尝试了流 reader:
FileStream fs = new FileStream("file.xml", FileMode.Open, FileAccess.Read);
xmldoc.Load(fs);
但它也抛出同样的错误。 那么如何以只读模式访问我的 XML 文档?
更新
我也尝试了 XPathDocument
和 FileStream("file.xml", FileMode.Open, FileAccess.Read,FileShare.Read)
。但是都没有解决问题
鉴于您已经说过 FileShare.Read
不起作用,其他进程似乎已打开文件进行写入。
您可以尝试使用 FileAccess.Read
和 FileShare.ReadWrite
打开它,在这种情况下,如果其他进程确实写入文件,您将需要处理可能发生的任何错误。
如果这不起作用,则可能是其他进程使用 FileShare.None
打开了它,在这种情况下,您无能为力。要检查这一点,请尝试使用记事本打开文件。
But is it still possible for FileShare.ReadWrite to throws error if it works in most cases?
如果另一个进程已经使用 FileShare.None
打开文件,您只会收到错误消息。您已确认在 Microsoft Word 中打开时不会出现这种情况,因此您应该没问题。
此 class 以只读模式显示已读 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[]>();
}
}