winfrom窗體工具查詢修改web.config丶xml格式配置
1.介面:
2.選項檔案按鈕:
/// <summary>
/// 選擇檔案
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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.查詢按鈕
/// <summary>
/// 查詢按鈕
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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 = "<add key=";
this.textBox2.Text = el.Attributes["key"].Value;
// this.label2.Text = " value =";
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.修改按鈕
/// <summary>
/// 修改
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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 = "<add key=";
//this.textBox2.Text = el.Attributes["key"].Value;
//this.label2.Text = " value =";
//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 = "<add key=";
//this.textBox4.Text = el.Attributes["key"].Value;
//this.label7.Text = " value =";
//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;
}
}