I am trying to append to an .xml file using the code[1] below, but I keep coming across the error[2]. Any idea what could be causing it? Thanks.
[1]
[2]
[1]
Code:
private bool insertName()
{
try
{
XmlDocument XmlDoc = new XmlDocument();
XmlDoc.Load(XmlFilePath);
XmlElement NameElement = XmlDoc.CreateElement("name");
XmlText UserName = XmlDoc.CreateTextNode("Jemima");
NameElement.AppendChild(UserName);
XmlDoc.Save(XmlFilePath); // line 69
return true;
}
catch (Exception Excep)
{
MessageBox.Show(Excep.ToString());
return false;
}
}
Code:
System.IO.IOException: The process cannot access the file 'C:\Users\Chris\Documents\Visual Studio 2008\Projects\name\name\bin\Debug\names.xml' because it is being used by another process. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share) at System.Xml.XmlTextWriter..ctor(String filename, Encoding encoding) at System.Xml.XmlDocument.Save(String filename) at WindowsFormsApplication1.nameCheck.insertName() in C:\Users\Chris\Documents\Visual Studio 2008\Projects\name\name\name.cs:line 69
Comment