110,561
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;
using System.Drawing.Imaging;
using System.Data.SqlClient;
using System.Windows.Forms.Integration;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
String filePath = "";
private void openFile_Click(object sender, EventArgs e)
{
DataTable dataTable1 = new DataTable();
System.Data.DataRow dr;
dataTable1.Columns.Add(new System.Data.DataColumn("key", typeof(System.String)));
dataTable1.Columns.Add(new System.Data.DataColumn("value", typeof(System.String)));
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
filePath = openFileDialog1.FileName;
if (!filePath.ToLower().EndsWith("web.config"))
{
MessageBox.Show("不是web.config");
return;
}
XmlDocument doc = new XmlDocument();
doc.Load(filePath);
XmlNodeList add = doc.SelectNodes("//appSettings/add");
foreach (XmlNode node in add)
{
dr = dataTable1.NewRow();
dr[0] = node.Attributes["key"].Value;
dr[1] = node.Attributes["value"].Value;
dataTable1.Rows.Add(dr);
}
this.dataGridView1.DataSource = dataTable1;
}
}
private void saveButton_Click(object sender, EventArgs e)
{
XmlDocument doc = new XmlDocument();
doc.Load(filePath);
XmlNode appSettings = doc.SelectSingleNode("//appSettings");
appSettings.RemoveAll();
XmlNodeList add = doc.SelectNodes("//appSettings/add");
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (dataGridView1.Rows[i].Cells[1].Value != null && dataGridView1.Rows[i].Cells[0].Value != null)
{
String key = dataGridView1.Rows[i].Cells[0].Value.ToString();
String value = dataGridView1.Rows[i].Cells[1].Value.ToString();
if (key.Equals("") || value.Equals("")) return;
XmlNode newNode = doc.CreateElement("add");
XmlAttribute attKey = doc.CreateAttribute("key");
attKey.Value = key;
newNode.Attributes.Append(attKey);
XmlAttribute attVaue = doc.CreateAttribute("value");
attVaue.Value = value;
newNode.Attributes.Append(attVaue);
appSettings.AppendChild(newNode);
}
doc.Save(filePath);
}
}
}
}
using System;
using System.Xml;
using System.IO;
namespace Sdyy_DQPZWJ
{
/// <summary>
/// Config 的摘要说明。
/// </summary>
public class Config
{
private string filename;
private string configcreatefile;
private XmlDocument xdoc;
public void Save()
{
if (xdoc != null) xdoc.Save(filename);
}
public Config(string FileName)
{
//
// TODO: 在此处添加构造函数逻辑
//
this.filename = FileName;
if (File.Exists(filename))
{
xdoc = new XmlDocument();
xdoc.Load(filename);
}
else //没有该文件
{
//创建一个新文件
try
{
XmlTextWriter tw = new XmlTextWriter(filename, System.Text.Encoding.UTF8);
tw.WriteStartDocument();
tw.WriteStartElement("Config");
tw.WriteEndElement();
//tw.Close();
}
catch (Exception ee)
{
//文件不能创建,发出警告,提示用户
//System.Windows.Forms.MessageBox.Show("不能创建文件 - " + filename + ee.ToString());
System.Windows.Forms.MessageBox.Show(configcreatefile + filename + ee.ToString());
}
}
}
//写字符参数
public void WriteString(string section, string key, string strVal)
{
if (xdoc == null) return;
XmlNode newNode = xdoc.CreateElement(section);
newNode.Attributes.Append(xdoc.CreateAttribute(key));
newNode.Attributes.GetNamedItem(key).InnerText = strVal;
xdoc.DocumentElement.AppendChild(newNode);
}
//读字符参数
public string ReadString(string section, string key)
{
string val = "";
if (xdoc == null) return val;
System.Xml.XmlNodeList xlist = xdoc.DocumentElement.GetElementsByTagName(section);
foreach (System.Xml.XmlNode i in xlist)
{
System.Xml.XmlNode xmlnode = i.Attributes.GetNamedItem(key);
if (xmlnode != null) val = xmlnode.Value;
}
return val;
}
public void WriteString1(string section, string key, string strVal)
{
string val = "";
if (xdoc == null) return;
System.Xml.XmlNodeList xlist = xdoc.DocumentElement.GetElementsByTagName(section);
foreach (System.Xml.XmlNode i in xlist)
{
System.Xml.XmlNode xmlnode = i.Attributes.GetNamedItem(key);
if (xmlnode != null) xmlnode.Value = strVal;
}
this.Save();
}
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string fName = "e:\\Codes\\temp\\littleProgram\\ASP.NET\\Test1\\web.config"; //配置文件的绝对路径
private void button1_Click(object sender, EventArgs e)
{
try
{
XmlNode node = GetElementInConfigurations(fName, "appSettings", "add", "key", "test");
if (node != null)
label1.Text = node.Attributes["value"].Value;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void button2_Click(object sender, EventArgs e)
{
try
{
XmlNode node = GetElementInConfigurations(fName, "appSettings", "add", "key", "test");
if (node != null)
{
node.Attributes["value"].Value = textBox1.Text;
SaveXml(fName, node);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private XmlNode GetElementInConfigurations(string fileName, string section, string nodeName, string keyName, string key)
{
XmlDocument doc = new XmlDocument();
doc.Load(fileName);
XmlNode root = doc.DocumentElement; //根结点,即configuration结点
foreach (XmlNode sectionNode in root.ChildNodes)
{
if (sectionNode.Name == section)
foreach (XmlNode node in sectionNode.ChildNodes)
if (node.Name == nodeName && node.Attributes[keyName].Value == key)
return (node);
}
return (null);
}
void SaveXml(string fileName,XmlNode node)
{
if (node != null)
node.OwnerDocument.Save(fileName);
}
}
private void button1_Click(object sender, EventArgs e)
{
DataTable dataTable1 = new DataTable();
System.Data.DataRow dr;
dataTable1.Columns.Add(new System.Data.DataColumn("key", typeof(System.String)));
dataTable1.Columns.Add(new System.Data.DataColumn("value", typeof(System.String)));
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
String f = openFileDialog1.FileName;
if (!f.ToLower().EndsWith("web.config"))
{
MessageBox.Show("不是web.config");
return;
}
XmlDocument doc = new XmlDocument();
doc.Load(f);
XmlNodeList add = doc.SelectNodes("//appSettings/add");
foreach (XmlNode node in add)
{
dr = dataTable1.NewRow();
dr[0] = node.Attributes["key"].Value;
dr[1] = node.Attributes["value"].Value;
dataTable1.Rows.Add(dr);
}
this.dataGridView1.DataSource = dataTable1;
}
}