-1

Intermittently, I see an IO exception at XMLDocument.Save(). Initially, I thought it's the same as question in the following links: 1. XmlDocument.Save unhandled exception thrown from GC thread 2. Memory exception while XDocument.Save()

But, My call stack is different from the ones discussed in above threads. Here is my call stack: ------- Base Exception (start here) --------- Root cause may be due to this exception: 'System.IO.IOException' Message=IOException,

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)

Problem is my IOException doesn't quote any reason. Following is the code:

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);

Wanted to understand more on this. Any help will be appreciated.

Community
  • 1
  • 1
  • 1
    What is the actual error (not call stack)? It might be a permission issue or simultaneous access issue. Can you check if you can open the file? Verify that nothing else is accessing the file when you're running your code too – Tyress Feb 01 '17 at 04:10
  • Thanks Tyress. We couldn't check this for the reason that this test was run in a VM and has already got shut down when we started the triage. I've checked at other IOexceptions, they usually give a clear message indicating what caused the exception i.e,Diskfull, Illegal access, etc., – Praneeth Kumar Gunda Feb 01 '17 at 05:25
  • Is more than one user writing to file or your application writing the same file more than once? This looks like a window exception that the user can't write file because it was already opened. Make sure you dispose the Filestream before opening the file a 2nd time. – jdweng Feb 01 '17 at 10:50

1 Answers1

0

Jon Skeet in an answer to similar question(see https://stackoverflow.com/a/8354736/4151626), states that there is probably a bug in XmlDocument.Save() that incorrectly pins the stream. By taking direct control of the creation and disposition of the stream outside of the XmlDocument.Save() method I was able to get around this halting error.

//e.g.
XmlWriter xw = new XmlWriter.Create(traceFilename);
traceXml.Save(xw);
xw.Close();
xw.Dispose();
ergohack
  • 1,268
  • 15
  • 27