Assume that an XML file is as follows:
<? XML version = "1.0" encoding = "UTF-8"?> <! -- Note: In addition to manually editing this file, you can also use web management tools to configure application settings. You can use the "website"> "ASP. NET configuration" option in Visual Studio. The complete list of settings and comments is displayed on the machine. config. in comments, the file is usually located in \ windows \ Microsoft. net \ framework \ v2.0.xxxxx \ config --> <configuration> <deleetask> <add key = "startupfilewatch" value = "true"/> <add key = "maxsize" value =" 2097152 "/> <add key =" minsize "value =" 10 "/> <add key =" watchdir "value =" uploadfolder "/> <add key =" ignorepostfix "Value = ".html#.htm ;. JS "/> <add key =" cscommand "value =" 1 "/> <add key =" pecommand "value =" 1 "/> </appsettings> </configuration>
/// <Summary> /// whether to enable real-time monitoring /// </Summary> static public bool startupfilewatch {get {string STR = xml. getconfig ("startupfilewatch", @ "config \ topwincms. filewatcher. config "); Return convert. toboolean (STR);} set {XML. savesetting ("startupfilewatch", value. tostring (), @ "config \ topwincms. filewatcher. config ");}}
/// <Summary> /// read the specified node value in XML /// </Summary> /// <Param name = "strkeyname"> </param> /// <returns> </returns> Public static string getconfig (string strkeyname) {return getconfig (strkeyname, "Web. config ");} /// <summary> /// read the specified node value in XML /// </Summary> /// <Param name = "strkeyname"> node key name </param> /// <returns> specify the node key value </returns> Public static string getconfig (string strkeyname, string strwhich) {string configpath; try {configpath = string. concat (system. web. httpruntime. appdomainapppath, @ "\", strwhich);} catch {configpath = system. windows. forms. application. startuppath; If (configpath. endswith (@ "\ bin") {configpath = configpath. substring (0, configpath. length-4);} configpath = string. concat (configpath, @ "\", strwhich);} using (xmltextreader TR = new xmltextreader (configpath) {While (TR. read () {If (TR. nodetype = xmlnodetype. element) {If (TR. name = "add" & tr. getattribute ("key") = strkeyname) {return tr. getattribute ("value") ;}}} return NULL ;}
/// <Summary> /// Save the web. config /// </Summary> /// <Param name = "strkeyname"> node name </param> /// <Param name = "strkeyvalue"> value to be saved </param> /// <Param name = "strwhich"> which configuration file </param> Public static void savesetting (string strkeyname, string strkeyvalue, string strwhich) {// create an XML document instance system. XML. xmldocument xmlwebsetting = new system. XML. xmldocument (); // open the XML document xmlwebsetting. load (system. web. httpruntime. appdom Ainapppath + "/" + strwhich); // find the node location system. XML. xmlnodelist = xmlwebsetting. selectsinglenode ("// appsettings "). childnodes; foreach (system. XML. xmlnode Xn in xmlnodelist) {If (Xn. attributes ["key"]. innertext = strkeyname) {xn. attributes ["value"]. innertext = strkeyvalue; xmlwebsetting. save (system. web. httpcontext. current. server. mappath ("~ /"+ Strwhich +" "); break ;}}}