XML-based configuration file access interface design and implementation (1)

zhaozj2021-02-16  95

XML-based configuration file access interface design and implementation (1)

table of Contents

Summary

Profile structure

Implementation of XMLConfigReader class

XMLConfigReader class

Summary

During program development, some program settings / usage are often stored. Since this information and program setting / use, it is quite independent with the program, so it is impossible to hardcode into the program. In this When we choose to use XML-based profiles to store. Microsoft's .NET Framework provides a series of readsTTings, such as read-based files, such as the appSettings provided by System.Configuration namespace. But this namespace is provided Classs can only read the configuration file, can not be set. So here, we implement a class XMLConfigReader / XMLConfigWriter based on our XML-based profile.

Configuration file structure

In order to achieve compatibility with the original, systematic profile, we choose to use the structure similar to the .config file. Examples are as follows:

All the information to be set is placed in sub-nodes of the Configuration node (such as AppSettings / Sockbasesettings) sub-node, which helps to categorize / unity of information different settings. Structure and system .config structure Basically similar. This makes it easy to convert this custom structure into .config files.

Implementation of XMLConfigReader class

The basic structure of the current file has been completed, and now the encoding is started, and XMLConfigReader is completed.

Since the configuration file is placed on the hard disk in the form of a file, this XMLConfigReader class gets the path to the file before parsing the XML file.

Public Class XmlconfigReader

{

PRIVATE STRING _FILEPATH;

Public XMLConfigReader (String filepath) {

_filepath = path.getfullpath (filepath) .toupper ();

}

}

Ok, now you can get the file path. Then it is parsing the configuration file. Here, we use the .NET Framework provided by the lightweight XMLTextReader in the system.xml namespace to make the configuration file. Corresponding XMLConfigReader The function is defined as follows:

Public String Process (String SectionName, String Key) {BOOL Inconfiguration = FALSE

BOOL INSECTION = FALSE;

String Values;

XMLTextReader Reader = New XMLTextReader (_FilePath);

While (Reader.Read ()) {

IF (reader.isstartElement ()) {

IF (reader.prefix == String.empty)

{

IF (reader.localname == "configuration")

{

Inconfiguration = true;

}

Else if (Inconfiguration == True) {

IF (reader.localname == sectionname) {

insection = true;

}

Else IF (INSECTION && Reader.LocalName == "add") {

IF (Reader.GetaTribute ("key") == null || reader.getattribute ("value") == null)

{

Throw new Exception (SectionName "Key or Value Is Null");

}

IF (Reader.GetaTribute ("key") == key) {

VALUES = Reader.GetaTRibute ("Value");

Break;

}

}

}

}

}

}

Reader.Close ();

Return Values;

}

The nodes in the XML file are traversed by XMLTextReader's READ () function. At the same time, it is first determined whether it belongs to the Configuration node, and then determines whether it belongs to the corresponding sectionName. Only when these two parts are set up, it is necessary to determine if it is the corresponding Key. If so, get it Value and return.

XMLConfigReader class

Ok, now you can read the configuration file through XMLConfigReader. Here we look at the actual code:

Public class testxmlconfigreader {

Public void getValues ​​() {

XmlconfigReader Reader = New XMLConfigRead (@ "appconfig.xml");

String Temp;

// Get Appsettings UserName Value

Temp = Reader.Process ("Appsettings", "UserName");

// Get Sockbasesettings Serverip Value

Temp = Reader.Process ("SockBaseSettings", "Serverip");

}

}

to sum up

With the XMLConfigReader class, we can easily customize our own profile.

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

New Post(0)