zoukankan      html  css  js  c++  java
  • winform利用读取xml获取webconfig

    一、利用读取xml获取web.config中的数据库连接

    参考:传送门

     #region 利用读取xml获取webconfig
    private static String GetXML()
    {
    //待修改
    String f = System.Windows.Forms.Application.StartupPath;
    f = f.Replace("调试", "CommonMethod\\Web.config");//对路径进行处理
    XmlDocument doc = new XmlDocument();
    doc.Load(f);
    XmlNodeList add = doc.SelectNodes("//appSettings/add");
    String DBString = "";
    foreach (XmlNode node in add)
    {
    if (node.Attributes["key"].Value.Equals("strConn"))
    {
    DBString = node.Attributes["value"].Value;
    break;
    }

    }
    return DBString;
    }



    二、获取webconfig的路径

    System.IO.Directory.GetCurrentDirectory() 
    这个方法,会随着你的当前系统路径的改变而改变.比如你打开一个openFileDialog那么,再次获得路径就不对了。
    string m_PathStr = Application.ExecutablePath;
    是能获得全部,包括exe文件名的一串东西比如c:\abc\abc.exe
    Application.StartupPath
    能够获得当前的运行路径,不包括执行文件名
    比如c:\abc\(又没有那个“\|”不记得了)
    自己分析你要那个吧

    参考:传送门
    三、利用xml读写web.config

    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);
    }
    }

    }
    }



  • 相关阅读:
    洛谷—— P2234 [HNOI2002]营业额统计
    BZOJ——3555: [Ctsc2014]企鹅QQ
    CodeVs——T 4919 线段树练习4
    python(35)- 异常处理
    August 29th 2016 Week 36th Monday
    August 28th 2016 Week 36th Sunday
    August 27th 2016 Week 35th Saturday
    August 26th 2016 Week 35th Friday
    August 25th 2016 Week 35th Thursday
    August 24th 2016 Week 35th Wednesday
  • 原文地址:https://www.cnblogs.com/0banana0/p/2335727.html
Copyright © 2011-2022 走看看