1.界面:
2.选项文件按钮:
///
/// 选择文件
///
///
///
private void button3_Click(object sender, EventArgs e)
{
//初始化一个OpenFileDialog类
OpenFileDialog fileDialog = new OpenFileDialog();
//判断用户是否正确的选择了文件
if (fileDialog.ShowDialog() == DialogResult.OK)
{
//获取用户选择文件的后缀名
string extension = Path.GetFullPath(fileDialog.FileName);
this.textBox1.Text = extension;
MessageBox.Show(extension);
}
}
3.查询按钮
///
/// 查询按钮
///
///
///
private void button1_Click(object sender, EventArgs e)
{
#region 1
XmlDocument xmldoc = new XmlDocument();
try
{
xmldoc.Load(textBox1.Text);
//获取节点列表
//得到顶层节点列表
XmlNodeList topM = xmldoc.DocumentElement.ChildNodes;
foreach (XmlNode xn in topM)
{
XmlElement element = xn as XmlElement;//最好不好用强制转换,建议使用as,这样如果转不过去话,就是null,不会报异常
if (element != null)
{//这样判断以下是否为null,或者你可以if(xe==null) return ;直接返回或做其他处理
if (element.Name.ToLower() == "appsettings")
{
//得到该节点的子节点
XmlNodeList nodelist = element.ChildNodes;
if (nodelist.Count > 0)
{
//DropDownList1.Items.Clear();
foreach (XmlNode xn1 in nodelist)//读元素值
{
XmlElement el = xn1 as XmlElement;
if (el != null)
{
if (el.Attributes["key"].Value == "ConnectionString")
{
//this.label1.Text = "
this.textBox3.Text = el.Attributes["value"].Value;
//this.label3.Text = "/>";
//el.Attributes["value"].Value = this.textBox3.Text;
}
if (el.Attributes["key"].Value == "FtpPath")
{
this.textBox4.Text = el.Attributes["key"].Value;
this.textBox5.Text = el.Attributes["value"].Value;
//el.Attributes["value"].Value = this.textBox5.Text;
}
if (el.Attributes["key"].Value == "Category")
{
this.textBox6.Text = el.Attributes["key"].Value;
this.textBox7.Text = el.Attributes["value"].Value;
//el.Attributes["value"].Value = this.textBox5.Text;
}
}
}
}
}
}
}
xmldoc.Save(@"C:\Users\Administrator\Desktop\Web.config");
// xmldoc.Attributes["value"].Value = this.textBox3.Text;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "系统提示!");
return;
}
#endregion
}
4.修改按钮
///
/// 修改
///
///
///
private void button2_Click(object sender, EventArgs e)
{
XmlDocument xmldoc = new XmlDocument();
try
{
xmldoc.Load(textBox1.Text);
//获取节点列表
//得到顶层节点列表
XmlNodeList topM = xmldoc.DocumentElement.ChildNodes;
foreach (XmlNode xn in topM)
{
XmlElement element = xn as XmlElement;//最好不好用强制转换,建议使用as,这样如果转不过去话,就是null,不会报异常
if (element != null)
{//这样判断以下是否为null,或者你可以if(xe==null) return ;直接返回或做其他处理
if (element.Name.ToLower() == "appsettings")
{
//得到该节点的子节点
XmlNodeList nodelist = element.ChildNodes;
if (nodelist.Count > 0)
{
//DropDownList1.Items.Clear();
foreach (XmlNode xn1 in nodelist)//读元素值
{
XmlElement el = xn1 as XmlElement;
if (el != null)
{
if (el.Attributes["key"].Value == "ConnectionString")
{
//this.label1.Text = "
//this.textBox3.Text = el.Attributes["value"].Value;
//this.label3.Text = "/>";
el.Attributes["value"].Value = this.textBox3.Text;
}
if (el.Attributes["key"].Value == "FtpPath")
{
//this.label6.Text = "
//this.textBox5.Text = el.Attributes["value"].Value;
//this.label8.Text = "/>";
el.Attributes["value"].Value = this.textBox5.Text;
}
if (el.Attributes["key"].Value == "Category")
{
//this.textBox6.Text = el.Attributes["key"].Value;
//this.textBox7.Text = el.Attributes["value"].Value;
el.Attributes["value"].Value = this.textBox7.Text;
}
MessageBox.Show("修改成功!", "系统提示!");
}
}
}
}
}
}
xmldoc.Save(@"C:\Users\Administrator\Desktop\Web.config");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "系统提示!");
return;
}
}