1. xml file format
<?xml version="1.0" encoding="utf-8"?><userInfo> <userName>sa</userName> <userPwd>111111</userPwd></userInfo>
2. Key code 2.1 muserinfo. CS
//************************************** ******************************** // Assembly: XML operation // Author: Amy // created: 02-17-2014 // last modified by: Amy/last modified on: 02-17-2014 //********************************* **************************************// <copyright file = "muserinfo. CS "Company =" Microsoft "> // copyright (c) Microsoft. all rights reserved. // </Copyright> // <summary> </Summary> //*********************** **************************************** * ******* namespace XML operation {// <summary> // class muserinfo. /// </Summary> public class muserinfo {// <summary> // the user name // </Summary> private string username = string. empty; // <summary> // The User Password // </Summary> private string userpwd = string. empty; // <summary> // gets or sets the name of the user. /// </Summary> /// <value> the name of the user. </value> Public String username {get {return username;} set {username = value ;}/// <summary> // gets or sets the user password. /// </Summary> /// <value> the user password. </value> Public String userpwd {get {return userpwd;} set {userpwd = value ;}}}}
2.2 xmloperate. CS
//************************************** ******************************** // Assembly: XML operation // Author: Amy // created: 02-17-2014 // last modified by: Amy/last modified on: 02-18-2014 //********************************* **************************************// <copyright file = "xmloperate. CS "Company =" Microsoft "> // copyright (c) Microsoft. all rights reserved. /// </Copyright> // <summary> </summa Ry> //************************************ * ********************************** using system. XML; namespace XML operation {// <summary> // class xmloperate. /// </Summary> public class xmloperate {// <summary> // loads the XML. /// </Summary> /// <Param name = "xmlfilename"> name of the xmlfile. </param> /// <returns> system. string. </returns> Public static string loadxml (string xmlfilename) {string result = String. empty; xmldocument xml = new xmldocument (); XML. load (@ xmlfilename); Return XML. innerxml. tostring () ;}/// <summary> // saves the XML. /// </Summary> /// <Param name = "xmlcontent"> content of the XML. </param> // <Param name = "xmlfilename"> name of the XML file. </param> Public static void savexml (string xmlcontent, string xmlfilename) {xmldocument xml = new xmldocument (); XML. loadxml (xmlconten T); XML. save (@ xmlfilename);} // <summary> // gets the user information by executable ml. /// </Summary> /// <Param name = "FILENAME"> name of the file. </param> // <returns> muserinfo. </returns> Public static muserinfo getuserinfobyxml (string filename) {muserinfo model = new muserinfo (); xmldocument xml = new xmldocument (); XML. load (@ filename); xmlnode xmlusername = xml. selectsinglenode ("userinfo/ Username "); If (xmlusername! = NULL) {model. Username = xmlusername. innertext. Trim ();} xmlnode xmluserpwd = xml. selectsinglenode ("userinfo/userpwd"); If (xmluserpwd! = NULL) {model. userpwd = xmluserpwd. innertext. trim ();} return model;} // <summary> // sets the user information automatic XML. /// </Summary> /// <Param name = "fliename"> name of the flie. </param> /// <Param name = "model"> the model. </param> Public static void setuserinfotoxml (string fliename, muserinfo model) {xmldocument xml = new xmldocument (); XML. load (@ fliename); xmlnode xmlusername = xml. se Lectsinglenode ("userinfo/username"); If (xmlusername! = NULL) {xmlusername. innertext = model. Username;} xmlnode xmluserpwd = xml. selectsinglenode ("userinfo/userpwd"); If (xmluserpwd! = NULL) {xmluserpwd. innertext = model. userpwd;} XML. Save (@ fliename );}}}