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
XML Version = "1.0" encoding = "UTF-8"?>
...
appsettings>
configure>
Then read it in the program.
String connString = configurationSettings.appsettings ["connectionstring"];
This method is not a simple and clear lightweight method when the system scale is less complex, but if the system is more complicated, the configuration item will be more, and we need to divide the configuration according to different modules, and It is also hoped that it can be packaged in an object-oriented method, and if this too much simplified method is still 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
{
Get {return connectionstring;
Set {connectionString = value;
}
Since the AppConfig class 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 the 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, you can get the object directly.
IF (config == NULL)
{
// New sequential 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 subject to object
Config = (appconfig) Serial.Deselialize (fs);
fs.close ();
// Add object to the cache
HttpContext.current.cache.Insert ("Appconfig", Config, New Cached CachedPendency;
}
Catch (System.IO.FilenotFoundException)
{
CONFIG = New Appconfig ();
}
}
Return Config;
}
/ / Save the configuration object
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.Serialize (fs, config);
fs.close ();
}
/ / Get the configuration file path
Private static 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 of the class is very simple, the basic method is as follows
// code is only for the demo
Appconfig CONFIG = AppconfigSetting.get ();
String connString = config.connectionstring;
...
Config.connectionstring = connString;
AppconfigSetting.save (config);