2013-04-05 78 views
0

将大量XML文件导入到应用程序后,我尝试使用XML文档类对其进行修改,为此我创建了几个方法来执行修改。保存C#中的XML文档时出现System.IO异常

事情是开始的方法,它的工作正常,当谈到第二个它显示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; } 
    } 

感谢, 拉吉

+0

如果您演示如何管理XML文件,这将有所帮助。 – 2013-04-05 10:02:36

+4

没有任何代码很难分辨,但我敢打赌,在第一次编辑之后你并没有处理文件,所以在GC扫描之前它不会被关闭...... – 2013-04-05 10:02:51

+0

你将不得不发布你的代码。很明显,你只是缺少一两个命令,但是如果没有你现在使用的代码,我们怎么知道世界上的? – 2013-04-05 10:06:11

回答

0

你可能在你的应用程序中打开同一个文件。在您再次打开它之前,您必须关闭它(或者将其打开并在不打开的情况下处理同一文档)。

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

+0

如何关闭? – user1805169 2013-04-05 10:12:43

相关问题