我有一大批数据要处理,在这一大批数据当中,查找到一些数据后写入xml,相当于前面有好多方法检查,一旦查到,就调用写xml的这个方法如下,保存;
xml正常
XmlDocument newdoc = new XmlDocument();
newdoc.load("a.xml");
...中间添加节点
newdoc.save("a.xml");
因为是对硬盘上的文件进行的检查,所以是一个循环.我在本地的PC上跑这个数据,刚开始没问题,但有时跑着跑着,就会报a.xml已占用, 未处理的“System.IO.IOException”类型的异常出现在 mscorlib.dll 中,,这样的错.但我就开了一个界面winform写的,在跑这个程序.
这是怎么回事呀?但有时我要把它放到服务器上,处理能力好的服务器时,有时就不报,这是怎么回事?内存没释放?但我的确是每次都执行save啦呀,save不会释放这个load来的xml吗?如何解决这个问题?

解决方案 »

  1.   

    应该及时把文件关闭。
    用using试试
    using( XmlDocument newdoc = new XmlDocument(); )
    {
    }
      

  2.   

    hhhh63 怎么关闭,我load,然后save,还怎么关闭?
      

  3.   

    报错,编辑不通过.
    无法将类型system.xml.xmldocument 隐式转化为system.idisposable  
    xmldocument 没有实现idisposable  的接口吧.
      

  4.   

    报错,编辑不通过.
    无法将类型system.xml.xmldocument 隐式转化为system.idisposable  
    xmldocument 没有实现idisposable  的接口吧.
      

  5.   

    多贴一点代码出来看看,可能是你写入xml的时候有问题
      

  6.   

    public static void CreateSingerXml( string singername,string singerid,string matchname,string matchid,bool flag )
    { //生成新文件
    XmlDocument newdoc = new XmlDocument()

    string newpath = Directory.GetCurrentDirectory();
    newpath = newpath+"/xml/singerlist.xml"; string path1 = "e:\\singername.xml"; if ( !File.Exists(path1))
    newdoc.Load(newpath);
    else
    newdoc.Load(path1);

    XmlNode node1 = newdoc.SelectSingleNode("//singerlist"); XmlNode node4 = newdoc.CreateElement("singer");
    node1.AppendChild(node4); XmlNode node3 = newdoc.CreateElement("singername");
    node3.InnerText = singername;
    node4.AppendChild(node3); XmlNode node2 = newdoc.CreateElement("singerid");
    if ( flag == false )
    node2.InnerText = singerid;
    else
    node2.InnerText = "0"; node4.AppendChild(node2);
    XmlNode matchnode = newdoc.CreateElement("Match");
    node4.AppendChild(matchnode); XmlNode matchsingername = newdoc.CreateElement("matchsingername");
    if ( flag == true )
    matchsingername.InnerText = matchname;
    else
    matchsingername.InnerText = "N"; matchnode.AppendChild(matchsingername); XmlNode matchsingerid = newdoc.CreateElement("matchsingerid");
    if ( flag == true )
    matchsingerid.InnerText = matchid;
    else
    matchsingerid.InnerText = "0"; matchnode.AppendChild(matchsingerid);
    XmlNode flagmatchnode = newdoc.CreateElement("IsAllMatch");
    flagmatchnode.InnerText = flag.ToString();
    matchnode.AppendChild(flagmatchnode); newdoc.Save(path1);
    }
      

  7.   

    两种方式:
    1.使用lock和using,锁定并及时释放
    2. 
    using (FileStream reader = File.Open(xmlPath,FileMode.Open,FileAccess.Read,FileShare.ReadWrite))
                {
                    
                                              
                }
    使用共享型文件读取方式如果是多并发,建议使用第一种方式,第二种方式无法保证并发情况的数据正确性
      

  8.   

    XmlDocument没有实现IDisposable是不能用using的。
    加lock看看,或者用11楼讲的2的方法试试。
    关注中