写XML出现写了一半 如何防止

ttlrwdl 2007-12-28 03:48:17
可能写XML文件比较频繁 文件大于200K以上 就会出现 文件添加节点只有一般的情况 如何防止
...全文
140 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
seamanhy 2008-02-13
  • 打赏
  • 举报
回复
我说的可能和主题关系不大,但应该也是有用。

是不是等写入结束后,可以检查下节点,如果错误就尝试重新写???
亡羊补牢啊。
CCsdnCC 2008-02-12
  • 打赏
  • 举报
回复
doc.Save(filepath2);
******* doc.Flush() *****;
doc = null;
ttlrwdl 2007-12-28
  • 打赏
  • 举报
回复
没有 我这样写的
XmlDocument doc = new XmlDocument();
XmlDeclaration xmldecl;
xmldecl = doc.CreateXmlDeclaration("1.0", "gb2312", null);
doc.AppendChild(xmldecl);
XmlElement root = doc.CreateElement("c" + touin);
doc.AppendChild(root);
XmlAttribute attr = doc.CreateAttribute(null, "MsgCounts", null);
root.Attributes.Append(attr);
root.Attributes.GetNamedItem("MsgCounts").InnerXml = "2";
XmlAttribute T_attr = doc.CreateAttribute(null, "lastTime", null);
root.Attributes.Append(T_attr);
root.Attributes.Item(1).InnerXml = DateTime.Now.ToString();
root.AppendChild(doc.CreateElement("c" + time.ToShortDateString()));
XmlNode tp = root.SelectSingleNode("c" + time.ToShortDateString());
tp.AppendChild(doc.CreateElement("id" + time.ToString("yyyyMMddhhmmssfff")));
tp = tp.SelectSingleNode("id" + time.ToString("yyyyMMddhhmmssfff"));
tp.AppendChild(doc.CreateElement("id"));

tp = tp.SelectSingleNode("id");
tp.InnerText = time.ToString("yyyyMMddhhmmssfff");
doc.Save(filepath2);
doc = null;
要写的节点比较多 我省了 有时候会碰到写了几个会丢失 楼上 能否说详细点

CCsdnCC 2007-12-28
  • 打赏
  • 举报
回复
有没有调用Flush()?

110,567

社区成员

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

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

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