A good class for reading the configuration file, which is convenient for setting the configuration file

Source: Internet
Author: User
I haven't pasted things for a long time. I can paste things on demand to increase popularity!

Class: config. CS
Using system;
Using system. xml;
Using system. reflection;
Using system. configuration;
Using system. Windows. forms;

Namespace xmlconfig
{
Public class config: system. configuration. etettingsreader
{
Private xmlnode node;
Private string _ partition file;

Public String Upload File
{
Get {return _ response file ;}

Set {_ prop file = application. startuppath + "\" +
Value ;}
}

Public String getvalue (string key)
{
Return convert. tostring (getvalue (Key, typeof (string )));
}

Public new object getvalue (string key, system. Type stype)
{
Xmldocument Doc = new xmldocument ();
Object RO = string. empty;
Loaddoc (DOC );
String snode = key. substring (0, key. lastindexof ("//"));
// Retrieve the selected node
Try
{
Node = Doc. selectsinglenode (snode );
If (node! = NULL)
{

// XPath selects element that
Contains the key

Xmlelement targetelem =
(Xmlelement) node. selectsinglenode (key );
If (targetelem! = NULL)
{

RO =
Targetelem. getattribute ("value ");
}
}
If (stype = typeof (string ))
Return convert. tostring (RO );
Else
If (stype = typeof (bool ))
{

If (Ro. Equals ("true") |
Ro. Equals ("false "))

Return
Convert. toboolean (RO );
Else
Return false;
}
Else
If (stype = typeof (INT ))
Return convert. toint32 (RO );
Else
If (stype = typeof (double ))
Return convert. todouble (RO );
Else
If (stype = typeof (datetime ))
Return convert. todatetime (RO );
Else
Return convert. tostring (RO );
}
Catch
{
Return string. empty;
}
}

Public bool setvalue (string key, string Val)
{
Xmldocument Doc = new xmldocument ();
Loaddoc (DOC );
Try
{
// Retrieve the target node

String snode = key. substring (0,
Key. lastindexof ("//"));
Node = Doc. selectsinglenode (snode );
If (node = NULL)
Return false;
// Set element that contains the key

Xmlelement targetelem = (xmlelement)
Node. selectsinglenode (key );
If (targetelem! = NULL)
{
// Set new value

Targetelem. setattribute ("value ",
Val );
}

// Create new element
Key/value pair and add it
Else
{


Snode =
Key. substring (key. lastindexof ("//") + 2 );


Xmlelement entry =
Doc. createelement (snode. substring (0, snode. indexof ("[@"). Trim ());

Snode =
Snode. substring (snode. indexof ("'") + 1 );


Entry. setattribute ("key ",
Snode. substring (0, snode. indexof ("'")));

Entry. setattribute ("value", Val );
Node. appendchild (entry );
}
Savedoc (Doc, this. _ saved file );
Return true;
}
Catch
{
Return false;
}
}

Private void savedoc (xmldocument doc, string docpath)
{
// Save document
// Choose
Ignore if Web. config since it may cause Server sessions interruptions
If (this. _ your file. Equals ("Web. config "))
Return;
Else
Try
{

Xmltextwriter writer = new
Xmltextwriter (docpath, null );

Writer. Formatting =
Formatting. indented;
Doc. writeto (writer );
Writer. Flush ();
Writer. Close ();
Return;
}
Catch
{}
}

Public bool removeelement (string key)
{
Xmldocument Doc = new xmldocument ();
Loaddoc (DOC );
Try
{

String snode = key. substring (0,
Key. lastindexof ("//"));
// Retrieve the specified ettings Node
Node = Doc. selectsinglenode (snode );
If (node = NULL)
Return false;

// XPath select setting "add" element that contains
This key to remove
Xmlnode nD = node. selectsinglenode (key );
Node. removechild (ND );
Savedoc (Doc, this. _ saved file );
Return true;
}
Catch (exception ex)
{
Console. writeline (ex. Message );
Return false;
}
}

Private void loaddoc (xmldocument DOC)
{
// Check for type of config file being requested
/*
If (this. _ partition file. Equals ("app. config "))
{
// Use default app. config

This. _ Upload File =
(Assembly. getentryassembly (). getname (). Name + ". EXE. config ";
}
Else
If (this. _ your file. Equals ("Web. config "))
{
// Use Server Web. config

This. _ Upload File =
System. Web. httpcontext. Current. server. mappath ("Web. config ");
}
*/
// Load the document

Doc. Load (this. _ Upload File );
}

}
}

Usage:
Take:

Config = <SPAN class = "CS-keyword"> New </span> config (); <br> config. export File = <SPAN class = "CPP-string"> "app. config "</span>; <br> txtcountry. TEXT = <br> config. getvalue (<SPAN class = "CPP-string"> "// deletemetadata // Add [@ key = 'countryloc ']" </span> );

Write:

Config. setvalue (<SPAN class = "CPP-string"> "// appsettings // Add [@ key = '" </span> <br> + txtkey. text + <SPAN class = "CPP-string"> "']" </span>, txtvalue. text); <br> <SPAN class = "CS-comment"> </span> <br>

Delete:

Config. removeelement (<SPAN class = "CPP-string"> "// appsettings // Add [@ key = '" </span> + <br> txtkey2.text + <SPAN class =" CPP-string ">" '] "</span> );

For more information, see: http://www.codeproject.com/csharp/XmlConfig.asp

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.