C# XML文件写入问题
我试图使用下面的代码将child附加到XML文件中C# XML文件写入问题,c#,xml,C#,Xml,我试图使用下面的代码将child附加到XML文件中 string controlFolderPath = string.Empty; var currentPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); if (currentPath.Contains("\\bin\\Debug")) currentPath = currentPath.Replace("\\bin\\Debug", "");
string controlFolderPath = string.Empty;
var currentPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
if (currentPath.Contains("\\bin\\Debug"))
currentPath = currentPath.Replace("\\bin\\Debug", "");
else if (currentPath.Contains("\\bin\\Release"))
currentPath = currentPath.Replace("\\bin\\Release", "");
string strFilename = currentPath + "\\InitialSetup\\ClientList.xml";
XmlDocument xDoc = new XmlDocument();
xDoc.Load(strFilename);
var node = xDoc.DocumentElement.SelectSingleNode("./Client[@Name='" + clientName.Trim() + "']");
if (node != null)
{
MessageBox.Show("The entered Client Name already exists.");
return false;
}
XmlElement clientElement = xDoc.CreateElement("Client");
clientElement.SetAttribute("Name", clientName.Trim());
xDoc.DocumentElement.AppendChild(clientElement);
xDoc.Save(strFilename);
MessageBox.Show("Client " + clientName.Trim() + " added in system");
但是,我不断收到错误,指出“该进程无法访问文件,因为它被另一个进程使用”
有人能帮我解决这个错误吗?请以后更多地注意您的格式设置-代码无缘无故地大量缩进,需要用户水平滚动才能看到大部分内容。错误消息是不言自明的@是的,我会遵守的。Thanks@RahulSingh是的,我知道这是不言自明的,但文件没有保持打开状态。请重新启动电脑,然后重试