0

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

公共类注册{列出用户;列出新用户;字符串用户路径 = string.Empty; 字符串 NewUserpath = string.Empty; 字符串 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;
        }
    }
}

提前致谢...

4

1 回答 1

0

看起来你永远不会关闭你的阅读器,你需要在某个时候调用 xr.Close() 。或者按照 Johan 的建议,将其包装在 using 语句中:

    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 库,但这里似乎有些不对劲。您可能会考虑将其进一步分解。

于 2013-07-25T15:34:33.130 回答