Mitchell

Add location element into web.config programmatically by XMLDocument Object

<%@ Page Language="C#" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Xml" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        string fileName = Server.MapPath("web.config");
        XmlDocument xDoc = new XmlDocument();
        xDoc.Load(fileName);

        XmlElement newLocationNode = xDoc.CreateElement("location");
        newLocationNode.SetAttribute("path", "default2.aspx");
        xDoc.DocumentElement.AppendChild(newLocationNode);

        XmlElement inwebNode = xDoc.CreateElement("system.web");
        newLocationNode.PrependChild(inwebNode);

        XmlElement inauthorizationNode = xDoc.CreateElement("authorization");
        inwebNode.PrependChild(inauthorizationNode);

        XmlElement inallowNode = xDoc.CreateElement("allow");
        inallowNode.SetAttribute("roles", "common users");
        inauthorizationNode.PrependChild(inallowNode);

        XmlElement indenyNode = xDoc.CreateElement("deny");
        indenyNode.SetAttribute("users", "user1,user2");
        inauthorizationNode.AppendChild(indenyNode);

        xDoc.Save(fileName);

        Response.Write("updated");
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
    </div>
    </form>
</body>
</html>

posted on 2008-03-18 18:54  MitChell  阅读(169)  评论(0编辑  收藏  举报

导航