c++ cli - How to find given content of node and modify xml file using C++/CLI -
loginsandpasswords.xml looks this:
<?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 insert login name loginbox
. before add new user want check if login exists in xml file. example check if login named loginbox->text=b
exists in xml file. if yes want show messagebox("given login exists choose another")
. if no want create new user
in xml file given unique login(loginbox->text
), given password(passwordbox->text
) , id greater max id value of users.
this should help:
private: system::void button1_click(system::object^ sender, system::eventargs^ e) { //1 validation condition: none textbox can empty bool emptyrgstbox = false; if (loginbox->text == ""){ messagebox::show("login box can not empty!"); emptyrgstbox = true; } if (passwordbox->text == ""){ messagebox::show("password box can not empty!"); emptyrgstbox = true; } //2 validation condition: check if given passwords same bool passwordsarethesame = true; if (passwordbox->text != passwordconfirmationbox->text) { messagebox::show("passwords not same, try again"); passwordbox->text = ""; passwordconfirmationbox->text = ""; passwordsarethesame = false; } //3 validation condition: check if given login 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; (int = 0; < lstusers->count; i++) { if (loginbox->text == lstusers[i]->childnodes[1]->innertext) { messagebox::show("login exists, choose login"); logintaken = true; } } } else { messagebox::show(l"the file " + strfilename + l" not found"); } //checks whether conditions have been met if (passwordsarethesame == true && emptyrgstbox == false && logintaken==false) { //finding greates id of users int maxid = 0; if (file::exists(strfilename)) { docloginsandpasswords->load(strfilename); xmlelement ^ elm = docloginsandpasswords->documentelement; xmlnodelist ^ lstusers = elm->childnodes; (int = 0; < 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 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 = ""; } } }
Comments
Post a Comment