C# 在C中保存XML文档时发生System.IO异常#

C# 在C中保存XML文档时发生System.IO异常#,c#,xml,C#,Xml,将大量XML文件导入应用程序后,我尝试使用XML文档类对其进行修改,为此我创建了一些方法进行修改 问题是启动方法,它工作正常,当涉及到第二个时,它会显示System.IO异常,如“文件已在使用另一个进程” 所以有谁能帮我解决这个问题 我正在做的示例代码: Method1(fileList); Method2(fileList); Method3(fileList); private void Method1(IList<RenamedImportedFileInfo> fi

将大量XML文件导入应用程序后,我尝试使用XML文档类对其进行修改,为此我创建了一些方法进行修改

问题是启动方法,它工作正常,当涉及到第二个时,它会显示System.IO异常,如“文件已在使用另一个进程”

所以有谁能帮我解决这个问题

我正在做的示例代码:

Method1(fileList);
Method2(fileList);
Method3(fileList);

    private void Method1(IList<RenamedImportedFileInfo> fileList)
    {
           try
        {

            string isDefaultAttribute = Resource.Resources.ImportIsDefaultAttribute;
            string editorsPath = editorsFolderName + Path.DirectorySeparatorChar + meterType;
            string profilesPath = profileFolderName + Path.DirectorySeparatorChar + meterType;
            string strUriAttribute = Resource.Resources.ImportUriAttribute;

            foreach (RenamedImportedFileInfo renameInfo in fileList)
            {
                if (renameInfo.NewFilePath.ToString().Contains(editorsPath) && (renameInfo.IsProfileRenamed != true))
                {
                    var xmldoc = new XmlDocument();
                    xmldoc.Load(renameInfo.NewFilePath);

                    if (xmldoc.DocumentElement.HasAttribute(isDefaultAttribute))
                    {
                        xmldoc.DocumentElement.Attributes[isDefaultAttribute].Value = Resource.Resources.ImportFalse;
                    }

                    XmlNodeList profileNodes = xmldoc.DocumentElement.GetElementsByTagName(Resource.Resources.ImportMeasurementProfileElement);
                    if (profileNodes.Count == 0)
                    {
                        profileNodes = xmldoc.DocumentElement.GetElementsByTagName(Resource.Resources.ImportBsMeasurementProfileElement);
                    }
                    if (profileNodes.Count > 0)
                    {
                        foreach (RenamedImportedFileInfo profileName in oRenamedImportedFileList)
                        {
                            if (profileName.NewFilePath.ToString().Contains(profilesPath))
                            {
                                if (string.Compare(Path.GetFileName(profileName.OldFilePath), Convert.ToString(profileNodes[0].Attributes[strUriAttribute].Value, CultureInfo.InvariantCulture), StringComparison.OrdinalIgnoreCase) == 0)
                                {
                                    profileNodes[0].Attributes[strUriAttribute].Value = Path.GetFileName(profileName.NewFilePath);
                                    renameInfo.IsProfileRenamed = true;
                                    break;
                                }
                            }
                        }
                    }

                    xmldoc.Save(renameInfo.NewFilePath);
                    xmldoc = null;
                    profileNodes = null;
                }
            }
            oRenamedImportedFileList = null;
        }
        catch (NullReferenceException nullException) { LastErrorMessage = nullException.Message; }
    }
Method1(文件列表);
方法2(文件列表);
方法3(文件列表);
私有void方法1(IList文件列表)
{
尝试
{
字符串isDefaultAttribute=Resource.Resources.ImportIsDefaultAttribute;
字符串editorsPath=editorsFolderName+Path.directoryseportorchar+meterType;
字符串profilesPath=profileFolderName+Path.directoryseportorchar+meterType;
string strUriAttribute=Resource.Resources.ImportUriAttribute;
foreach(重命名导入文件信息重命名文件列表中的信息)
{
if(rename info.NewFilePath.ToString().Contains(editorsPath)&&(rename info.isprofilerename!=true))
{
var xmldoc=新的XmlDocument();
Load(重命名info.NewFilePath);
if(xmldoc.DocumentElement.HasAttribute(isDefaultAttribute))
{
xmldoc.DocumentElement.Attributes[isDefaultAttribute].Value=Resource.Resources.ImportFalse;
}
XmlNodeList profileNodes=xmldoc.DocumentElement.GetElementsByTagName(Resource.Resources.ImportMeasurementProfileElement);
if(profileNodes.Count==0)
{
profileNodes=xmldoc.DocumentElement.GetElementsByTagName(Resource.Resources.ImportBsMeasurementProfileElement);
}
如果(profileNodes.Count>0)
{
foreach(在oRenamedImportedFileList中重命名导入文件信息配置文件名)
{
if(profileName.NewFilePath.ToString().Contains(profilesPath))
{
if(string.Compare(Path.GetFileName(profileName.OldFilePath)),Convert.ToString(profileNodes[0]。Attributes[strUriAttribute]。Value,CultureInfo.InvariantCulture),StringComparison.OrdinalIgnoreCase==0)
{
profileNodes[0]。属性[strUriAttribute]。值=路径。GetFileName(profileName.NewFilePath);
rename info.isprofilerename=true;
打破
}
}
}
}
xmldoc.Save(重命名info.NewFilePath);
xmldoc=null;
profileNodes=null;
}
}
oRenamedImportedFileList=null;
}
catch(NullReferenceException nullException){LastErrorMessage=nullException.Message;}
}
谢谢,
Raj

您可能在应用程序中打开同一文件两次。在再次打开之前,必须先将其关闭(或保持打开状态,然后在不再次打开的情况下处理同一文档)


有关如何实现此功能的帮助,请向我们展示更多代码,以便我们为您提供建议。

如果您演示如何管理XML文件,这将有所帮助。如果没有任何代码,很难说清楚,但我打赌您在第一次编辑后不会处理该文件,所以它不会关闭,直到GC扫描进来…你必须发布你的代码。很明显,你只是缺少一两条命令,但是如果没有你目前拥有的代码,我们怎么知道呢?请展示方法的代码,而不是你如何调用它们。上面我发布的是示例代码,我正在对其余方法进行修改