代码之家  ›  专栏  ›  技术社区  ›  Nathan Ridley

web.config自定义配置节和不必要的冗长

  •  1
  • Nathan Ridley  · 技术社区  · 16 年前

    除非我做错了什么,否则我应该使用ConfigurationSection、ConfigurationElement和ConfigurationElementCollection的方式将要求我按如下方式格式化我的配置节:

    <serviceAuthorization>
        <credentials>
            <login username="system" password="password" mode="include">
                <services>
                    <service type="AxeFrog.Mobile.Service.Security.AuthenticationService, AxeFrog.Mobile.Service" />
                    <service type="AxeFrog.Mobile.Service.Security.AnotherService, AxeFrog.Mobile.Service" />
                </services>
            </login>
            <login username="test" password="pass" mode="exclude" />
        </credentials>
    </serviceAuthorization>
    

    <serviceAuthorization>
        <login username="system" password="password" mode="include">
            <service type="AxeFrog.Mobile.Service.Security.AuthenticationService, AxeFrog.Mobile.Service" />
            <service type="AxeFrog.Mobile.Service.Security.AnotherService, AxeFrog.Mobile.Service" />
        </login>
        <login username="test" password="pass" mode="exclude" />
    </serviceAuthorization>
    

    有没有一种方法可以获取配置部分的XML并自己阅读?

    2 回复  |  直到 16 年前
        1
  •  1
  •   Josef Pfleger    16 年前

    您可以实施 System.Configuration.IConfigurationSectionHandler 并对其进行配置:

    <section name="serviceAuthorization" type="[your-type]"/>
    

    然后你得到你的全部 section 作为 XmlNode

    编辑 new way

        2
  •  0
  •   Vinay Sajip    16 年前

    例如,你可以这样做:

    string docName=System.Web.HttpContext.Current.Server.MapPath("Web.config");
    XmlDocument configDoc = new XmlDocument();
    configDoc.Load(docName);
    

    然后工作 configDoc .

    推荐文章