该进程无法访问文件'xxx.xml',因为它正由另一个进程使用

时间:2013-07-25 12:17:07

标签: asp.net-mvc-4

我搜索了很多,但我得到任何问题的解决方案。 我试图将一个节点添加到xxx.xml文件中,但它抛出一个错误 “进程无法访问文件'xxx.xml',因为它正被另一个进程使用”,下面是我的类

公共课注册     {         列出用户;         列出NewUsers;         string Userpath = string.Empty;         string NewUserpath = string.Empty;         string strUsername = string.Empty;

    public bool FINDUSERNAME(string firstname, string lastname, string emailaddress, string country, string purchasedate, string username, string password)
    {
        //Put code to get the offers from database to Offers variable
        if (ReadXML(firstname, lastname, emailaddress, country, purchasedate, username, password))
            return true;
        else
            return false;
    }

    //bool ReadXML(XmlDocument xmlfile2)
    bool ReadXML(string firstname, string lastname, string emailaddress, string country, string purchasedate, string username, string password)
    {
        try
        {
            XmlDocument receivedxml = new XmlDocument();
            Userpath = HttpContext.Current.Server.MapPath("/SampleData/Registration.xml");
            NewUserpath = HttpContext.Current.Server.MapPath("/SampleData/NewRegistration.xml");

            XmlReaderSettings xrs = new XmlReaderSettings();
            xrs.DtdProcessing = DtdProcessing.Ignore;
            XmlReader xr = XmlReader.Create(Userpath, xrs);
            if (xr != null)
            {
                //Setting the Root element
                XmlRootAttribute xRoot = new XmlRootAttribute();
                xRoot.ElementName = "Registration";
                xRoot.IsNullable = true;

                XmlSerializer deserializer = new XmlSerializer(typeof(Registration), xRoot);
                Registration UserDetails = (Registration)deserializer.Deserialize(xr);
                Users = UserDetails.Users;

                foreach (var varuser in Users)
                {
                    if (username == varuser.Username)
                    {
                        strUsername = varuser.Username;
                        return true;
                    }
                }
                if (strUsername == "")
                {
                    //here iam trying to add a node to the xml
                    using (StreamWriter sw = new StreamWriter(File.Create(Userpath)))
                    {
                        sw.Write("<User><Firstname>"
                                + firstname + "</Firstname><Lastname>"
                                + lastname + "</Lastname><Country>"
                                + country + "</Country><Purchasedate>"
                                + purchasedate + "</Purchasedate><Emailaddress>"
                                + emailaddress + "</Emailaddress><Username>"
                                + username + "</Username><Password>"
                                + password + "</Password></User>");
                    }
                    return false;
                }
            }
            return false;
        }
        catch (Exception)
        {
            return false;
        }
    }
}

提前致谢...

1 个答案:

答案 0 :(得分:0)

看起来你永远不会关闭你的读者,你需要在某个时候调用xr.Close()。或者正如约翰建议的那样,将其包装在一个使用声明中:

    using (XmlReader xr = XmlReader.Create(Userpath, xrs))
    {
        //Setting the Root element
        XmlRootAttribute xRoot = new XmlRootAttribute();
        xRoot.ElementName = "Registration";
        xRoot.IsNullable = true;

        XmlSerializer deserializer = new XmlSerializer(typeof(Registration), xRoot);
        Registration UserDetails = (Registration)deserializer.Deserialize(xr);
        Users = UserDetails.Users;

        foreach (var varuser in Users)
        {
            if (username == varuser.Username)
            {
                strUsername = varuser.Username;
                return true;
            }
        }
        if (strUsername == "")
        {
            //here iam trying to add a node to the xml
            using (StreamWriter sw = new StreamWriter(File.Create(Userpath)))
            {
                sw.Write("<User><Firstname>"
                        + firstname + "</Firstname><Lastname>"
                        + lastname + "</Lastname><Country>"
                        + country + "</Country><Purchasedate>"
                        + purchasedate + "</Purchasedate><Emailaddress>"
                        + emailaddress + "</Emailaddress><Username>"
                        + username + "</Username><Password>"
                        + password + "</Password></User>");
            }
            return false;
        }
    }

另外注意:我注意到你的方法名为ReadXML,但你也在这个方法中编写XML。这可能令人困惑,你在读书还是写作?你的问题的一部分也可能是你打开文件进行阅读,然后创建文件进行写入?我以前没有处理过C#Xml库,但这里似乎没有。你可以考虑更多地打破这个。