如何使用 C++/CLI 查找节点的给定内容并修改 xml 文件

How to find given content of node and modify xml file using C++/CLI

LoginsAndPasswords.xml 看起来像这样:

<?xml version="1.0" encoding="Windows-1250"?>
<AllUsers>
  <User>
    <id>2</id>
    <login>a</login>
    <password>a</password>
  </User>
  <User>
    <id>5</id>
    <login>b</login>
    <password>b</password>
  </User>
  <User>
    <id>7</id>
    <login>c</login>
    <password>c</password>
  </User>
</AllUsers>

起初我在 loginBox 中插入了一些登录名。然后,在我添加新用户之前,我想检查该登录名是否已存在于我的 xml 文件中。例如,我想检查名为 loginBox->Text=b 的登录名是否已存在于 xml 文件中。如果是,我想显示 MessageBox("Given login already exists choose another")。如果不是,我想在我的 xml 文件中创建新的 User 给定的唯一登录名(loginBox->Text),给定的密码(passwordBox->Text)和 id 大于所有的最大 id 值用户。

这应该有帮助:

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {

             //1 VALIDATION CONDITION: None textBox can be empty
             bool emptyRgstBox = false;
             if (loginBox->Text == ""){ MessageBox::Show("Login box can not be empty!"); emptyRgstBox = true; }
             if (passwordBox->Text == ""){ MessageBox::Show("Password box can not be empty!"); emptyRgstBox = true; }

             //2 VALIDATION CONDITION: Check if given passwords are the same
             bool passwordsAreTheSame = true;
             if (passwordBox->Text != passwordConfirmationBox->Text)
             {
                 MessageBox::Show("Passwords are not the same, try again");
                 passwordBox->Text = "";
                 passwordConfirmationBox->Text = "";
                 passwordsAreTheSame = false;
             }

             //3 VALIDATION CONDITION: Check if given login already exists in db
             bool loginTaken = false;                                           
             String ^ strFilename = L"Files/LoginsAndPasswords.xml";
             XmlDocument ^ docLoginsAndPasswords = gcnew XmlDocument;

             if (File::Exists(strFilename))
             {
                 docLoginsAndPasswords->Load(strFilename);
                 XmlElement  ^ elm = docLoginsAndPasswords->DocumentElement;
                 XmlNodeList ^ lstUsers = elm->ChildNodes;

                 for (int i = 0; i < lstUsers->Count; i++)
                 {
                     if (loginBox->Text == lstUsers[i]->ChildNodes[1]->InnerText)
                     {
                         MessageBox::Show("Login already exists, choose another login");
                         loginTaken = true;
                     }
                 }

             }
             else
             {
                 MessageBox::Show(L"The file " + strFilename + L" was not found");
             }

             //Checks whether the conditions have been met
             if (passwordsAreTheSame == true && emptyRgstBox == false && loginTaken==false)
             {
                 //Finding greates id of all users
                 int maxId = 0;
                 if (File::Exists(strFilename))
                 {
                     docLoginsAndPasswords->Load(strFilename);
                     XmlElement  ^ elm = docLoginsAndPasswords->DocumentElement;
                     XmlNodeList ^ lstUsers = elm->ChildNodes;

                     for (int i = 0; i < lstUsers->Count; i++)
                     {
                         if (maxId < int::Parse(lstUsers[i]->ChildNodes[0]->InnerText))
                             maxId = int::Parse(lstUsers[i]->ChildNodes[0]->InnerText);
                     }
                     //MessageBox::Show("maxId= " + maxId);
                     maxId++;
                 }

                 //Adding new user to db 
                 if (File::Exists(strFilename))
                 {
                     docLoginsAndPasswords->Load(strFilename);

                     XmlElement ^ Element = docLoginsAndPasswords->CreateElement(L"User");
                     String ^ strUser = L"<id>" + maxId + L"</id>" +
                         L"<login>" + loginBox->Text + L"</login>" +
                         L"<password>" + passwordBox->Text + "</password>";

                     Element->InnerXml = strUser;
                     docLoginsAndPasswords->DocumentElement->AppendChild(Element);

                     docLoginsAndPasswords->Save(strFilename);
                     MessageBox::Show("User has been added sucessfully!");

                     //Cleans texBoxes
                     passwordBox->Text = "";
                     passwordConfirmationBox->Text = "";
                     loginBox->Text = "";

                 }
             }


}