操作XML文件提示正由另一进程使用

cftcy 2009-04-28 10:42:44
RT。。
折磨我一天了。我确定没有别的进程访问文件。而且是单线程的。


XmlDocument doc1 = new XmlDocument();
doc1.Load(Server.MapPath("~/info/consultative.xml"));

XmlDocument doc = (XmlDocument)doc1.Clone();
doc1 = null;

XmlNodeList nodeList = doc.SelectNodes("consultative/info");
foreach (XmlNode node in nodeList)
{
foreach (XmlNode nodeChild in node.ChildNodes)
{
if (nodeChild.Name == "uuid" && nodeChild.InnerText != ViewState["uuid"].ToString())
break;
if (nodeChild.Name == "answer")
nodeChild.InnerText = FCKeditor1.Value;
if (nodeChild.Name == "question")
nodeChild.InnerText = FCKeditor2.Value;
}
}

doc.Save(Server.MapPath("~/info/consultative.xml"));
doc = null;


每次都是doc.save时报错。

后来我用linq

String xmlPath = Server.MapPath("~/info/consultative.xml");

XDocument xd = XDocument.Load(xmlPath);
var query = from info in xd.Descendants("info") select info;
foreach (var item in query)
{
if (item.Element("uuid").Value == ViewState["uuid"].ToString())
{
item.Element("answer").SetValue(FCKeditor1.Value);
item.Element("question").SetValue(FCKeditor2.Value);
}
else
continue;
}

xd.Save(xmlPath);


结果也是同样的问题
[IOException: 文件“D:\RollBack\Visual Studio 2008\GrWeb\AllLoad\info\consultative.xml”正由另一进程使用,因此该进程无法访问该文件。]
System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) +7712640
System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy) +1162
System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share) +66
System.Xml.XmlWriter.Create(String outputFileName, XmlWriterSettings settings) +117
System.Xml.Linq.XDocument.Save(String fileName, SaveOptions options) +142
System.Xml.Linq.XDocument.Save(String fileName) +6
manage_consultativeshow.Button1_Click(Object sender, EventArgs e) in d:\RollBack\Visual Studio 2008\GrWeb\AllLoad\manage\consultativeshow.aspx.cs:90
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565
...全文
423 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
aierda 2009-04-29
  • 打赏
  • 举报
回复
定义一个全局变量,当读写XML文件时进行加锁
lock(_lock){}
cftcy 2009-04-29
  • 打赏
  • 举报
回复
我发现了单纯的修改文件没事。

是运行了几次增加后才出现文件锁定的。 增加我的做法是先插入空节点,然后转向修改的功能来添加新的内容

protected void Button1_Click(object sender, EventArgs e)
{
String suuid = GetRandom();

XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("~/info/consultative.xml"));
XmlNode node = doc.SelectSingleNode("consultative/info");

XmlElement newCase = doc.CreateElement("info");
XmlElement uuid = doc.CreateElement("uuid");
XmlElement title = doc.CreateElement("answer");
XmlElement content = doc.CreateElement("question");
uuid.InnerText = suuid;
title.InnerText = "";
content.InnerText = "";
newCase.AppendChild(uuid);
newCase.AppendChild(title);
newCase.AppendChild(content);
node.ParentNode.InsertBefore(newCase, node);

doc.Save(Server.MapPath("~/info/consultative.xml"));

Response.Redirect("consultativeshow.aspx?uuid=" + suuid);
}

用了一个笨方法解决了。
就是跳到中间页面,然后点一个链接操作过去。而不是直接Redirect。

不知道为什么这样就可以用。
好像逻辑是一样的啊。不知道什么地方在占用它。
chuxue1342 2009-04-29
  • 打赏
  • 举报
回复
你为什么要克隆一次节点,把:XmlDocument doc = (XmlDocument)doc1.Clone(); doc1 = null;删掉,直接用你的doc1对象!
ryb_cool 2009-04-29
  • 打赏
  • 举报
回复
其实并不是其它线程的原因!这个文件是被当前线程所占有,在你save之前,应该将所占用的资源释放掉,试一下!
cftcy 2009-04-29
  • 打赏
  • 举报
回复
是啊,我都重启vs,调试了好多次了。
只有两个页面在调用跳转。

而且开始能成功操作一次,下一次一模一样的操作就出错了
ryb_cool 2009-04-29
  • 打赏
  • 举报
回复
你真的排除没有另外一个进程在访问这个文件吗?或是文件已经写保护,你没有写的权限!

如果这两项全部排除的话,建议你用数据流的方式来实现你的目的!
cftcy 2009-04-29
  • 打赏
  • 举报
回复
我开始是这样写的。
后来才试试赋值给null会不会释放资源。

结果都不行。
peterb 2009-04-29
  • 打赏
  • 举报
回复
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("~/info/consultative.xml"));

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧