XmlDocument doesn't work after OpenFileDialog

mer

New Member
This is a really weird one. I was writing an app that writes an XML file. On some occasions however the file would not be created/overwritten. I managed to track down the specific events required to cause it to fail to write and I separated it out into a standalone program:\[code\]public partial class Form1 : Form{ public Form1() { InitializeComponent(); bool doFileOpenFirst = false; if (doFileOpenFirst) { OpenFileDialog dialog = new OpenFileDialog(); dialog.CheckFileExists = true; dialog.Filter = "Image files|*.bmp;*.jpg;*.png"; dialog.ShowDialog(); } // Just write a trivial XML file XmlDocument doc = new XmlDocument(); XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "UTF-8", null); doc.AppendChild(dec);// Create the root element XmlElement root = doc.CreateElement("Database"); doc.AppendChild(root); doc.Save("Trivial.xml"); }}\[/code\]Now if you run this, you will see it initially works. Now make doFileOpenFirst true. Before it writes the XML it will present you with a dialog for opening a file. If you use this dialog to select a file (any file; not "Trivial.xml"), the XML Save afterwards will fail. Silently.If you hit cancel in the OpenFileDialog, the save will work fine.So there's some issues with a file handle here perhaps, but what is the workaround? You will see that forcing a Dispose of OpenFileDialog doesn't help.
 
Back
Top