Simple XML reading and Modification

Source: Internet
Author: User

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 ;}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.