The purpose of this exercise is to use LINQ to XML, regular expressions, to practice using serialization and deserialization on this basis tomorrow, and continue to add a little bit of functionality.
First, this is a form program, designed as follows:
The XML that holds the user name and password is as follows:
The implementation code is as follows:
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.ComponentModel;4 usingSystem.Data;5 usingSystem.Drawing;6 usingSystem.Linq;7 usingSystem.Text;8 usingSystem.Threading.Tasks;9 usingSystem.Windows.Forms;Ten usingSystem.Text.RegularExpressions; One usingSystem.Xml; A usingSystem.Xml.Linq; - - namespaceCheckinfo the { - Public Partial classForm1:form - { - PublicForm1 () + { - InitializeComponent (); + } A at Private voidtextBox1_TextChanged (Objectsender, EventArgs e) - { - if(TextBox1.Text = ="Please enter user name, format: Qarootdc\\jqhuang") - { -TextBox1.Text =""; - } in - } to + Private voidTextbox2_textchanged_1 (Objectsender, EventArgs e) - { the if(TextBox2.Text = ="Please enter your password") * { $TextBox2.Text ="";Panax Notoginseng } - } the + Private voidButton1_Click (Objectsender, EventArgs e) A { the if(IsValidUserName (textbox1.text) = =false) + { -MessageBox.Show ("user name is not properly formatted! Please re-enter! "); $TextBox1.Text =""; $ } - Else - { the //The user name is formatted correctly. - checkuserandpwd (textbox1.text,textbox2.text);Wuyi } the } - Wu Private voidCheckuserandpwd (stringUsernamestringpwd) - { About //read Userinfo.xml detect if user exists $XDocument UserInfo = Xdocument.load (@"C:\Users\jqhuang\Desktop\UserInfo.xml"); - varresult = fromUserelementinchUserinfo.element ("System"). Element ("Users"). Elements ()whereUserelement.element ("username"). value.tostring () = = TextBox1.Text.ToString ()SelectUserelement.element ("pwd"). Value; - if(Result! =NULL) - { A foreach(varPasswordinchresult) + { the if(Password = =pwd) - { $MessageBox.Show ("user name and password match successfully! "); the } the Else the { theMessageBox.Show ("user name and password do not match, please re-enter the password"); -TextBox2.Text =""; in } the } the } About Else the { theMessageBox.Show ("the user you entered does not exist! "); the } + } - the BOOLIsValidUserName (stringuserName)Bayi { the returnRegex.IsMatch (UserName,@"^.+\\.+$"); the } - } -}
Run as follows--
1, the user name does not exist in the case:
/
2, the user name and password does not match the situation:
3, the user name format is not correct (with regular expression validation):
4, the user name and password matching successful situation:
Validation issues for user names and passwords in C #.