Read and write XML configuration files

Source: Internet
Author: User

Using System.Xml;

----------------------------------------------read out the node values in the XML file

XmlDocument xmldoc = new XmlDocument ();
Xmldoc.load (Application.startuppath + "\" + "STOExpress.ContentPush.WindowsService.Client.exe.config");
Get top-level node list
if (xmldoc.documentelement! = null) {
XmlNodeList topm = xmlDoc.DocumentElement.ChildNodes;
foreach (XmlElement el in from XmlElement element in topm where element. Name.tolower () = = "appsettings" SELECT element. ChildNodes into NodeList where nodelist. Count > 0 from XmlElement el in NodeList where el! = NULL select EL) {
if (el. attributes["Key"]. Value = = "SecurityKey") {
Txtsecuritykey.text = el. attributes["Value"]. Value;
}
if (el. attributes["Key"]. Value = = "Clientno") {
Txtclientcode.text = el. attributes["Value"]. Value;
}
if (el. attributes["Key"]. Value = = "ClientType") {
Txtclienttype.text = el. attributes["Value"]. Value;
}
}
}

---------------------------------------------write the value of the XML file

XmlDocument xmldoc = new XmlDocument ();

Xmldoc.load (Application.startuppath + "\" + "STOExpress.ContentPush.WindowsService.Client.exe.config");
Get top-level node list
if (xmldoc.documentelement! = null) {
XmlNodeList topm = xmlDoc.DocumentElement.ChildNodes;
foreach (XmlElement el in from XmlElement element in topm where element. Name.tolower () = = "appsettings" SELECT element. ChildNodes into NodeList where nodelist. Count > 0 from XmlElement el in NodeList where el! = NULL select EL) {
if (el. attributes["Key"]. Value = = "SecurityKey") {
El. attributes["Value"]. Value = TxtSecurityKey.Text.Trim ();
}
if (el. attributes["Key"]. Value = = "Clientno") {
El. attributes["Value"]. Value = TxtClientCode.Text.Trim ();
}
if (el. attributes["Key"]. Value = = "ClientType") {
El. attributes["Value"]. Value = TxtClientType.Text.Trim ();
}
}
}
Xmldoc.save (Configfilename);

Read and write XML configuration files

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.