0

私はよくグーグルで検索しましたが、問題の解決策はありません。xxx.xml ファイルにノードを追加しようとしていますが、 「別のプロセスで使用されているため、プロセスはファイル 'xxx.xml' にアクセスできません」というエラーがスローされます。以下は私のクラスです。

public class Registration { ユーザーのリスト。NewUsers を一覧表示します。文字列 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;
        }
    }
}

前もって感謝します...

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 に答える