Use the .NET XML serialization to solve the system configuration problem

zhaozj2021-02-16  62

In Web System development, we often need to read and set some system configuration items, common, such as database connection strings, upload paths, and more. In the initial ASP system, the comparison of common methods is to save values ​​as Application or session variables; in the ASP.NET system, currently more common simple methods are to write the corresponding configuration items into Web.config, for example

... then read in the program

String connString = configurationSettings.appsettings ["connectionstring"]; this method is not a simple and clear lightweight method when the system is smaller, but if the system is more complicated, the configuration item will be more, At the same time, we need to divide the configuration in different modules, and it is also hoped to encapsulate it in an object-oriented method, and if you still use this too much simplified method, it is not too suitable. -------------------------------------------------- ---------------, I tell a method of solving the system configuration problem by XML sequence. Details of XML serialization and reverse sequencing (another description are serialization and parallelization), you can view MSDN understanding; here is simple to say, XML serialization is serializing an object to an XML document The process, the reverse sequence is re-creating objects from the XML output. The intuitive performance is that this picture is as clear as shown in the following picture. It is clear. By serialization, it can use object-oriented methods, very natural and convenient reading and setting system configuration; .NET Framework assumes object and XML file mapping work We only need to use it to use OK. Let's talk about the specific content. The above shows that first require an XML configuration file, the format content is as shown in the figure, and the specific configuration item can be increased. Then we need to write a class, as shown in the figure; special point, in order to enable the class to implement XML serialization, you need to add attribute information XMLEMENT before all attribute declarations of the class, as shown below.

[XMLELEMENT] PUBLIC STRING CONNECTIONSTRING;} set {connectionString = value;}} Due to the Appconfig class itself does not implement a method, we need a configuration class appconfigSetting.cs. The structure of the class is simple, only two static methods, get () get Appconfig objects, save () saves the AppConfig object. In addition, we need to add the address of the XML configuration file in web.config.

public class appconfigSetting {// Get configuration object public static appconfig get () {// Try getting the object in the cache Appconfig config = (appconfig) httpContext.current.cache [Appconfig "]; // If there is no such configuration object in the cache, directly obtain the object if (config == null) {// new serialized object and specify its type XMLSerializer Serial = new xmlserializer (typeof (appconfig)); try {string file = httpContext.current.server.mAppath (getFile ()); // Read file stream FileStream Fs = new filestream (file, filemode.open); // File flow reverse sequence is object config = (appconfig) Serial.Deserialize (fs); fs.close (); // Add objects to cache httpcontext.current.cache.insert ("Appconfig", Config, New Cached Cachependence file));} catch (System.IO.FileNotFoundException) {config = new AppConfig ();}} return config;} // save the configuration objects public static void save (AppConfig config) {string file = HttpContext.Current.Server. Mappath (getFile ()); XMLSerializer Serial = New XMLSerializer (TypeOf (Appconfig)); filestream fs = new filestream (file, filemode.create); // Object sequence is file serial.serial Ize (fs, config); fs.close ();} // Get Profile Path Private STRING GETFILE () {string path = (string) httpcontext.current.cache ["filepath"]; if (path == null ) {path = configurationSettings.appsettings ["AppconfigPath"]; httpContext.current.cache ["filepath"] = path;} Return path;}} The use is very simple, the basic method is as follows

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

New Post(0)