首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >C#.Net: XML Document.Save抛出IOException

C#.Net: XML Document.Save抛出IOException
EN

Stack Overflow用户
提问于 2017-02-01 11:54:54
回答 1查看 799关注 0票数 0

偶尔,我会在XMLDocument.Save()上看到IO异常。最初,我认为这和以下链接中的问题是一样的: 1. XmlDocument.Save unhandled exception thrown from GC thread 2. Memory exception while XDocument.Save()

但是,我的调用堆栈与上述线程中讨论的不同。这是我的调用堆栈:-基本异常(从这里开始)-根本原因可能是由于这个异常:'System.IO.IOException‘Message=IOException,

代码语言:javascript
复制
StackTrace=   at System.IO.__Error.WinIOError(Int32 errorCode, String str)
   at System.IO.FileStream.WriteCore(Byte[] buffer, Int32 offset, Int32 count)
   at System.IO.FileStream.Write(Byte[] array, Int32 offset, Int32 count)
   at System.IO.StreamWriter.Flush(Boolean flushStream, Boolean flushEncoder)
   at System.IO.StreamWriter.Write(Char[] buffer)
   at System.IO.TextWriter.WriteLine()
   at System.Xml.XmlTextWriter.Indent(Boolean beforeEndElement)
   at System.Xml.XmlTextWriter.AutoComplete(Token token)
   at System.Xml.XmlTextWriter.WriteStartElement(String prefix, String localName, String ns)
   at System.Xml.XmlDOMTextWriter.WriteStartElement(String prefix, String localName, String ns)
   at System.Xml.XmlElement.WriteTo(XmlWriter w)
   at System.Xml.XmlElement.WriteContentTo(XmlWriter w)
   at System.Xml.XmlElement.WriteTo(XmlWriter w)
   at System.Xml.XmlElement.WriteContentTo(XmlWriter w)
   at System.Xml.XmlElement.WriteTo(XmlWriter w)
   at System.Xml.XmlDocument.WriteContentTo(XmlWriter xw)
   at System.Xml.XmlDocument.WriteTo(XmlWriter w)
   at System.Xml.XmlDocument.Save(String filename)

问题是我的IOException没有引用任何理由。代码如下:

代码语言:javascript
复制
XmlDocument traceXml = new XmlDocument();
traceXml.Load(traceFilename);
XmlElement root = t30Trace.DocumentElement;
XmlAttribute jobId = t30Trace.CreateAttribute("JobId");

jobId.Value = "45";
root.Attributes.Append(jobId);

traceXml.Save(traceFilename);

想要了解更多关于这方面的信息。任何帮助都将不胜感激。

EN

回答 1

Stack Overflow用户

发布于 2019-04-16 08:42:54

Jon Skeet在回答类似问题(请参阅https://stackoverflow.com/a/8354736/4151626)时指出,XmlDocument.Save()中可能存在错误地固定流的错误。通过在XmlDocument.Save()方法之外直接控制流的创建和处理,我能够避免这个停止错误。

代码语言:javascript
复制
//e.g.
XmlWriter xw = new XmlWriter.Create(traceFilename);
traceXml.Save(xw);
xw.Close();
xw.Dispose();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41971614

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档