2

I have a function that translates a xml file using a xsl style sheet. It does the job fine; but when I want to delete that transformed file sometimes I get the following error: System.IO.IOException: The process cannot access the file

The function is like this:

XslTransform transform = new XslTransform();

transform.Load('xsl_style_sheet');

transform.Transform('fullpath/xmlfilename','fullpath/transformedFileName')

XElement xEle = XElement.Load('fullpath/transformedFileName');

I do what ever with the xEle and in the end I want to delete the 'fullpath/transformedFileName' but some times i get the dreaded System.IO.IOException: The process cannot access the file

Can any one please help. A million thanks

8
  • The most likely cause is that a prior debugging instance didn't properly close the file. Is this happening in a production or development environment? Commented Aug 22, 2013 at 18:34
  • try running your project or visual studio as an administrator Commented Aug 22, 2013 at 18:35
  • I bet this is because the object the XElement uses to read the XML isn't always deallocated before you try to delete the file, and Windows refuses to do it because the file is still open when that happens. XElement isn't IDisposable, so I'm not too sure what you can do, short of trying again after some delay. Commented Aug 22, 2013 at 18:35
  • 2
    Another common cause is an anti-virus that doesn't use oplocks that is trying to scan the file when you are trying to delete it. Commented Aug 22, 2013 at 18:36
  • Is this 4.0 or up? XslTransform is obsolete... Commented Aug 22, 2013 at 18:40

1 Answer 1

2

Use the XslCompiledTranform class (XslTranform is obsolete ) and the overload on Transform that accepts an XmlReader and XmlWriter. You can call Dispose on them, they will take care of closing and disposing the underlying stream.

// Load the style sheet.
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load("xsl_style_sheet");

// Create the writer.
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.IndentChars = "\t";
using(XmlWriter writer = XmlWriter.Create("fullpath/transformedFileName", settings))
{
   using(XmlReader reader = XmlReader.Create("fullpath/xmlfilename"))
   {
     reader.MoveToContent();
     xslt.Transform(reader, writer);
   }
}

using(XmlReader reader = XmlReader.Create("fullpath/transformedFileName"))
{
   XElement xEle = XElement.Load(reader);
   // do all other stuff you need to do here


   // after this the file will be closed
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a million. I was wondering what does the settings.Indent = true; settings.IndentChars = "\t"; really do?
If it helped, please mark my answer. I copied the settings from the example on the msdn doc. You can leave it out if you don't want to have indentiation with tabs in it. You can use the XmlWriterSetting to control the encoding, xmldeclaration etc. see here
I have used the above code. Unfortunately, I sometimes still have the System.IO.IOException: The process cannot access the file error. I think it is the XElement that will not release the handle. Would you know how to dispose of the XElement, thus releasing the handle, once I dont need it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.