Customize an operation class for an XML configuration file

xiaoxiao2021-03-06  94

XML configuration file:

Fujian fujian_zyz Fujian_sql_zyz false

Implementation of operation:

Using system; using system.web; using system.text; using system.xml.serialization; using system.xml;

Namespace LTPTOOL {#Region Configuration Object Model Class

///

/// Li Tianping /// 2004.3 /// Configured MODUL class (note the type of related attribute and [XMLELEMENT]) /// use: modulesettings settings = moduleconfig.get settings (); /// public class ModuleSettings {private string datasource; private string uid; private string password; private bool showlogin; [XmlElement] public string DataSource {set {datasource = value;} get {return datasource;}} [XmlElement] public string Uid {set {uid = value;} get {return uid;}} [XmlElement] public string password {set {password = value;} get {return password;}} [XmlElement] public bool showLogin {set {showlogin = value; } Get {returnidelogin;}}

} #Endregion

#REGION configuration MODULECONFIG / / /

// / configured operation class moduleconfig. /// public class moduleconfig {

public static ModuleSettings GetSettings () {ModuleSettings data = null; XmlSerializer serializer = new XmlSerializer (typeof (ModuleSettings)); try {string fileName = "db.config"; FileStream fs = new FileStream (fileName, FileMode.Open); data = (ModuleSettings) serializer.Deserialize (fs); fs.Close ();} catch {data = new ModuleSettings ();} return data;} public static void SaveSettings (ModuleSettings data) {string fileName = "db.config"; XmlSerializer Serializer = new xmlserializer (typeof (modulesettings)); // serialize the object filestream fs = new filestream (filename, filemode.create); serializer.Serialize (fs, data); fs.close ();

#ndregion

}

}

Get detailed information on the configuration file;

ModuleSettingS settings = moduleconfig.get settings (); txtserver.text = settings.dataSource; txtuser.text = settings.uid; txtpass.text = settings.password;

Save changes:

Settings.datasource = txtserver.text; moduleconfig.savesettings (settings);

It is also very convenient for extended configurations, just modifying the modules of ModuleSettings and the file names in ModuleConfig. You can use any other configuration files.

转载请注明原文地址:https://www.9cbs.com/read-124390.html

New Post(0)