XML-based configuration file access interface design and implementation (2)
table of Contents
Summary
Implementation of XMLConfigWriter class
XMLConfigWriter 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.
Implementation of XMLConfigWriter class
Since the configuration file is to be written, and it is possible to write more times. So we don't use light XMLTextWriter, using XMLDocument.xmldocument to modify all XML nodes in memory, only wait until the explicit call save function The XML file will only be saved. When there is a lot of modifications, performance is better.
Similarly, first implement XMLConfigWriter constructor
Public Class XmlconfigWriter
{
PRIVATE STRING _FILEPATH;
Private xmldocument doc;
Public XMLConfigWriter (String Filepath)
{
_filepath = path.getFullPath (FilePath);
Doc = new xmldocument ();
Doc.Load (_filepath);
}
}
By constructor, the path of the configuration file is passed in, and the XMLDocument's Load method is called, and this file is loaded into memory.
Here we use the XMLDocument class. It implements the W3C Document Object Model (DOM) Level 1 Core (Core Dom Level 2). The DOM is the memory of the XML document (cache) tree representation, allowing navigation and editing of the document. With XMLDocument, we can easily operate nodes in memory.
For the writing of the configuration file, there is three kinds, one is new inserted a node, one is the modification of the existing node, the last one is to delete an existing node. We first start with the insertion. The code is as follows :
Private XMLNode CreateXmlNode (String localname) {
Return Doc.createnode (XMLNodettype.element, localname, "");
}
Private XMlattribute Createxmlattribute (String LocalName) {
Return Doc.createAttribute ("", localname, "");
}
Public void addsection (string section) {
XMLNode Secnode = doc.selectsinglenode ("/ configuration /" section);
IF (secnode! = null) {
Return;
}
Doc.documentelement.Appendchild (seenode (section);
}
Public void addkey (string section, string key) {
XMLNode Secnode = doc.selectsinglenode ("/ configuration /" section);
IF (Doc.selectsinglenode ("/ configuration /" section "/ add [@ key = /" " key " / "]")! = NULL) {
Return;
}
XMLNode Chi = CreatexmlNode ("add");
Xmlattribute Att = Createxmlattribute ("Key");
ATT.VALUE = KEY;
Chi.attributes.Append (att);
ATT = CREATEXMLATTRIBUTE ("Value");
ATT.Value = Value;
Chi.attributes.Append (att);
Secnotore.Appendchild (Chi);
}
For the insertion of the profile, there are two situations, one is inserting a new section node (ie the node like Appsettings / Sockbasesettings), one is inserted into a new ADD node under the current section node. In the above code, For the operation of the insertion node, it is first to determine if this is the same name node, if it exists, if it exists, directly RETURN, avoid creating the same name node. However, due to the final use of the ADD node is different The Section node, so it is only judged that the node below the same section node cannot be the same name.
If there is no node of the same name, add this new (through the CreateXMLNode function) node to the DOC object via the secnode.appenild function. At the same time, for the Add Node, it will be KEY / VALUE through the CreateXmlattributes.appent function. The property is added to this node. In this way, the insertion operation is complete.
Then we will complete the delete operation. Delete operation directly gets the parent node of the target node directly through the XMLDocument, and then deletes it through the XMLNode.RemoveChild operation. The code is as follows:
Public void deletesection (String section) {
XMLNode Secnode = doc.selectsinglenode ("/ configuration /" section);
Doc.documentelement.removechild (secnode);
}
Public void deleteKey (String section, string key) {
XMLNode Secnode = doc.selectsinglenode ("/ configuration /" section "/ add [@ key = /" " key " / "]");
IF (secnode! = null)
{
SECNODE.PARENTNODE.RemoveChild (Secnode);
}
}
Now start modifying. For modification, the idea is like this, first pass the XMLDocument's SelectsingLenode search to see if there is a node that satisfies the condition. If not, it is directly returnis, if present, then two cases. For the Add node, just modify directly Its Value property. For the section node, it is deleted by traversing all the sub-nodes (add nodes) below, then remove this section node, reproduce a new node (this new node's Name is to set. Value), then assign all the child nodes to this new node. The code is as follows:
Public void modifysection (String oldsection, string newsection) {
XMLNode Secnode = doc.selectsinglenode ("/ configuration /" ilsection); xmlnodelist list = secnode.childnodes;
Doc.documentelement.removechild (secnode);
Secnode = doc.createnode (XMLNodetype.element, NewSection, "");
Foreach (XMLNode I in List) {
Secnotore.Appendchild (i);
}
Doc.documentelement.Appendchild (secnode);
}
Public void modifyKey (String Section, String Key, String Value) {
XMLNode Secnode = doc.selectsinglenode ("/ configuration /" section "/ add [@ key = /" " key " / "]");
IF (secnode! = null)
{
Secnotore.attributes ["Value"]. Value = Value;
}
}
Ok, insert, modify, and delete operations are now basically completed, but now it is only in memory, not to operate the actual files. At this time, we have to modify the memory via the XMLDocument.save function. A good XML file is written to the file. The code is as follows:
Public void save () {
Doc.save (_filepath);
}
Public void save (String filepath)
{
Doc.save (FilePath);
}
XMLConfigWriter class
How to use it is simple. You first generate an XMLConfigWriter object, pass the configuration file into the configuration file, and then operate through the add / modify / delete and other functions. The code is as follows:
XmlconfigWriter Writer = new xmlconfigwriter (@ "appconfig.xml");
Writer.addsection ("appsettings");
Writer.AddKey ("Appsettings", "Serverip", "Localhost");
Writer.ModifyKey ("Appsettings", "Serverip", "127.0.0.1");
Writer.ModifySECTION ("Appsettings", "SockBasetdings");
Writer.deleteKey ("SockBaseSettings", "Serverip");
Writer.Deletesection ("sockbasesettings");
Writer.save ();
to sum up
We learned to use XMLDocument by writing XMLConfigWriter.