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

时间:2021-11-11 19:34:45

LoginsAndPasswords.xml looks like this:

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>

At first I insert some login name into loginBox. Then before I add new user I want to check if that login already exists in my xml file. For example I would like to check if login named loginBox->Text=b already exists in xml file. If yes I want to show MessageBox("Given login already exists choose another"). If no I want to create new User in my xml file with given unique login(loginBox->Text), given password(passwordBox->Text) and id greater than max id value of all users.

首先,我在loginBox中插入一些登录名。然后在我添加新用户之前,我想检查我的xml文件中是否已存在该登录。例如,我想检查名为loginBox-> Text = b的登录名是否已存在于xml文件中。如果是,我想显示MessageBox(“给定登录已经存在,选择另一个”)。如果不是我想在我的xml文件中使用给定的唯一登录(loginBox-> Text)创建新用户,给定密码(passwordBox-> Text)并且id大于所有用户的max id值。

1 个解决方案

#1


0  

This should help:

这应该有所帮助:

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 = "";

                 }
             }


}

#1


0  

This should help:

这应该有所帮助:

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 = "";

                 }
             }


}