现在是手工添加的,我想在程序中实现添加....

解决方案 »

  1.   

    ms-help://MS.MSDNQTR.2003FEB.2052/cpref/html/frlrfsystemxmlxmlnodeclassappendchildtopic.htm
      

  2.   

    我的思路:用编程方法对web。config文件进行读写操作
      

  3.   

    其实就是对xml文档的读写
      

  4.   

    或者 你可以看一下
    MS 的EnLib 内的 ConfigrationBlock 部分
      

  5.   

    XmlDocument xDoc=new XmlDocument();
    xDoc.Load(Server.MapPath("web.config")); XmlElement elem=xDoc.CreateElement("add"); XmlAttribute attrib=xDoc.CreateAttribute("key"); attrib.Value="ConnectionStrings";
    elem.Attributes.Append(attrib); attrib=xDoc.CreateAttribute("value");
    attrib.Value="data source=(local)";
    elem.Attributes.Append(attrib);
    xDoc.DocumentElement.ChildNodes[0].AppendChild(elem);
    xDoc.Save(Server.MapPath("web.config"));
    不过要注意,要让用户对WEB.CONFIG所在的文件夹有写的权限.
      

  6.   

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Configuration;namespace 易网科技.Admin.Article
    {
    /// <summary>
    /// ArticleInfo 的摘要说明。
    /// </summary>
    public class ArticleInfo : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.DataList DataList1;
    protected System.Web.UI.WebControls.CheckBox Single;
      protected DataOperation DataOP=new DataOperation();
    protected int Records;//记录数
    protected int TotalPage;//总页数
    protected int CurrentPage;//当前页
    protected int PageSize=Int32.Parse(ConfigurationSettings.AppSettings["PageSize"]);//每页记录数,此信息在Web.Config中
    protected DataSet ds;
    protected bool bPre,bNext;//是否启用
    protected string sPre,sNext;
    protected System.Web.UI.WebControls.Label Label1;
    protected CheckBox Full;//全选框
    protected Button Delete;//删除确认对话框 
    private void Page_Load(object sender, System.EventArgs e)
    {
    if(!this.IsPostBack)
    {
    if(Request["Page"]==null)
    {
    CurrentPage=1;

    }
    else
    {
    try
    {
    CurrentPage=Convert.ToInt32(Request["Page"]); }
    catch
    {
    CurrentPage=1;
    }
    }

    if(CurrentPage>0)
    {
    sPre="ArticleInfo.aspx?Page="+Convert.ToString(CurrentPage-1);//上一页
    sNext="ArticleInfo.aspx?Page="+Convert.ToString(CurrentPage+1);//下一页
    if(CurrentPage==1)
    {
    bPre=false;

    }
    else
    {
    bPre=true;

    }
    ds=DataOP.GetArticleData(CurrentPage,PageSize,out Records,out TotalPage);
    if(CurrentPage==TotalPage)
    {
    bNext=false;
    }
    else
    {
    bNext=true;
    }
    DataList1.DataSource=ds;
    DataList1.DataBind();
    }
    }
    } #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.DataList1.ItemCreated += new System.Web.UI.WebControls.DataListItemEventHandler(this.DataList1_ItemCreated_1);
    this.DataList1.PreRender += new System.EventHandler(this.DataList1_PreRender_1);
    this.DataList1.UpdateCommand += new System.Web.UI.WebControls.DataListCommandEventHandler(this.DataList1_UpdateCommand);
    this.DataList1.DeleteCommand += new System.Web.UI.WebControls.DataListCommandEventHandler(this.DataList1_DeleteCommand);
    this.DataList1.ItemDataBound += new System.Web.UI.WebControls.DataListItemEventHandler(this.DataList1_ItemDataBound_1);
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion private void DataList1_ItemCreated_1(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
    {  
    Full=(CheckBox)e.Item.FindControl("CheckBox1");
    //用户单击后弹出确认框
    Delete=(Button)e.Item.FindControl("Button1");
    }
          
    //因为数据库里保存的为数字,这里须要替换显示
    private void DataList1_ItemDataBound_1(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
    {
    if(e.Item.ItemIndex>-1)
    {
    int id,dl,xl,sh;
    id=Convert.ToInt32(ds.Tables[0].Rows[e.Item.ItemIndex]["cplbid"]);
    dl=Convert.ToInt32(ds.Tables[0].Rows[e.Item.ItemIndex]["dlid"]);
    xl=Convert.ToInt32(ds.Tables[0].Rows[e.Item.ItemIndex]["xlid"]);
    sh=Convert.ToInt32(ds.Tables[0].Rows[e.Item.ItemIndex]["sh"]);
    Label Id=(Label)e.Item.FindControl("Label5");
    Label BigClass=(Label)e.Item.FindControl("Label2");
    Label SmallClass=(Label)e.Item.FindControl("Label3");
    Label Pass=(Label)e.Item.FindControl("Label4");
    Id.Text=id.ToString();
    BigClass.Text=DataOP.GetBigClassName(dl);
    SmallClass.Text=DataOP.GetSmallClassName(xl);
    if(sh==1)
    Pass.Text="未通过";
    else
    Pass.Text="通过";
    }

    } private void DataList1_PreRender_1(object sender, System.EventArgs e)
    {
    Delete.Attributes.Add("onClick","javascrip:return confirm('删除后不可恢复!你真的确定吗?');");
    if(Full.Checked)
    {
    for (int i = 0; i < DataList1.Items.Count; i++)
    {
    CheckBox chk = (CheckBox)DataList1.Items[i].FindControl("CheckBox2");
    chk.Checked=true;
    }
    }
    else
    {
    for (int i = 0; i < DataList1.Items.Count; i++)
    {
    CheckBox chk = (CheckBox)DataList1.Items[i].FindControl("CheckBox2");
    chk.Checked=false;
    }
    }
    } private void DataList1_DeleteCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e)
    {
    ArrayList AllId=new ArrayList();
    for (int i = 0; i < DataList1.Items.Count; i++)
    {
    CheckBox chk = (CheckBox)DataList1.Items[i].FindControl("CheckBox2");
    if(chk.Checked==true)
    {
    Label id=(Label)this.DataList1.Items[i].FindControl("Label5");
    int Aid=Int32.Parse(id.Text);
    AllId.Add(Aid);
    }
    }
    if(DataOP.DeleteArticle(AllId)==false)
    {
    Response.Write("<font color=#ffoooo>删除失败....</font><a href=../AdminMG.aspx>请返回</a>");
    }
    else
    {
    Response.Redirect("ArticleInfo.aspx");
    }
    } private void DataList1_UpdateCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e)
    {
    ArrayList AllId=new ArrayList();
    for (int i = 0; i < DataList1.Items.Count; i++)
    {
    CheckBox chk = (CheckBox)DataList1.Items[i].FindControl("CheckBox2");
    if(chk.Checked==true)
    {
    Label id=(Label)this.DataList1.Items[i].FindControl("Label5");
    int Aid=Int32.Parse(id.Text);
    AllId.Add(Aid);

    }
    }
    if(DataOP.PassArticle(AllId)==false)
    {
    Response.Write("<font color=#ffoooo>审核失败....</font><a href=ArticleInfo.aspx>请返回</a>");
    }
    else
    {
    Response.Redirect("ArticleInfo.aspx");
    }
    }
    }
    }
      

  7.   

    public enum   ConfigFileType
    {
    WebConfig ,
    AppConfig
    } public class AppConfig : System.Configuration.AppSettingsReader

    public string  docName = String.Empty;
    private  XmlNode node=null; private int _configType; public   int ConfigType
     {
     get
     {
     return _configType;
     }
     set
     {
     _configType=value;
     }
     } public bool SetValue(string key, string value)
    {
    XmlDocument cfgDoc = new XmlDocument(); 
    loadConfigDoc(cfgDoc);
    // 返回appSettings节点
    node =  cfgDoc.SelectSingleNode("//appSettings");
       
    if( node == null )
    {
    throw new System.InvalidOperationException( "appSettings section not found"); 
    }
       
    try
    {
    // 先择key   
    XmlElement addElem= (XmlElement)node.SelectSingleNode("//add[@key='" +key +"']") ;
    if (addElem!=null)
    {
    addElem.SetAttribute("value",value);    
    }
    // 找不到,则添加key与value
    else 
    {
    XmlElement entry = cfgDoc.CreateElement("add");
    entry.SetAttribute("key",key);
    entry.SetAttribute("value",value); 
    node.AppendChild(entry);    
    }
    //保存
    saveConfigDoc(cfgDoc,docName);
    return true;
    }
    catch 
    {
    return false;
    }
    }
     
    private void saveConfigDoc(XmlDocument cfgDoc,string cfgDocPath)

    try
    {    
    XmlTextWriter writer = new XmlTextWriter( cfgDocPath , null );
    writer.Formatting = Formatting.Indented;     
    cfgDoc.WriteTo( writer );     
    writer.Flush();
    writer.Close();   
    return;
    }
    catch
    {
    throw;
    }
    }
     
    public bool removeElement ( string elementKey)
    {
    try
    {
    XmlDocument cfgDoc = new XmlDocument(); 
    loadConfigDoc(cfgDoc);
    // 返回appSettings Node
    node =  cfgDoc.SelectSingleNode("//appSettings");   
    if( node == null )
    {
    throw new System.InvalidOperationException( "appSettings section not found"); 
    }   
    // 移除所选
    node.RemoveChild( node.SelectSingleNode("//add[@key='" +elementKey +"']") );
       
    saveConfigDoc(cfgDoc,docName);
    return true;
    }
    catch
    {
    return false;
    }
    }
    private XmlDocument loadConfigDoc( XmlDocument cfgDoc )

    //加载文件
    if(  Convert.ToInt32(ConfigType)==Convert.ToInt32(ConfigFileType.AppConfig))
    {
        
    docName= ((Assembly.GetEntryAssembly()).GetName()).Name; 
    docName +=   ".exe.config";
    }
    else
    {
    docName=System.Web.HttpContext.Current.Server.MapPath("../../web.config");
    }
    cfgDoc.Load( docName ); 
    return cfgDoc;
    } }