Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# XML文件写入问题_C#_Xml - Fatal编程技术网

C# 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", "");

我试图使用下面的代码将child附加到XML文件中

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是的,我知道这是不言自明的,但文件没有保持打开状态。请重新启动电脑,然后重试