Mitchell

Create the web.config file programmatically

<%@ 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)
    {
        if (!Page.IsPostBack)
        {
            XmlDocument xmlDoc = new XmlDocument();
            //创建文档声明信息
            XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);

            //创建根节点
            XmlElement rootNode = xmlDoc.CreateElement("configuration");
            xmlDoc.InsertBefore(xmlDeclaration, xmlDoc.DocumentElement);
            xmlDoc.AppendChild(rootNode);

            XmlElement appSettingsNode = xmlDoc.CreateElement("appSettings");
            XmlElement connectionStringsNode = xmlDoc.CreateElement("connectionStrings");
            XmlElement webBeginNode = xmlDoc.CreateElement("system.web");

            xmlDoc.DocumentElement.PrependChild(appSettingsNode);
            rootNode.AppendChild(connectionStringsNode);
            rootNode.AppendChild(webBeginNode);

            XmlElement webCompilationNode = xmlDoc.CreateElement("compilation");
            webCompilationNode.SetAttribute("debug", "true");
            webBeginNode.PrependChild(webCompilationNode);

            XmlElement webauthenticationNode = xmlDoc.CreateElement("authentication");
            webauthenticationNode.SetAttribute("mode", "Forms");
            webBeginNode.AppendChild(webauthenticationNode);

            XmlElement locationNode = xmlDoc.CreateElement("location");
            locationNode.SetAttribute("path", "default.aspx");
            rootNode.AppendChild(locationNode);

            XmlElement inwebNode = xmlDoc.CreateElement("system.web");
            locationNode.PrependChild(inwebNode);

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

            XmlElement inallowNode = xmlDoc.CreateElement("allow");
            inallowNode.SetAttribute("roles", "administrator");
            inauthorizationNode.PrependChild(inallowNode);

            XmlElement indenyNode = xmlDoc.CreateElement("deny");
            indenyNode.SetAttribute("users", "*");
            inauthorizationNode.AppendChild(indenyNode);
           
           
            xmlDoc.Save(Server.MapPath("web.config"));

            Response.Write("XML file created");
        }

    }
</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:48  MitChell  阅读(143)  评论(0编辑  收藏  举报

导航