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"?>
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.