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

web.config自定义配置节和不必要的详细信息

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

    除非我做错了什么,否则我使用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 回复  |  直到 15 年前
        1
  •  1
  •   Josef Pfleger    15 年前

    你可以实现 System.Configuration.IConfigurationSectionHandler 并对其进行配置:

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

    然后你得到你的全部 section 作为 XmlNode 并且可以解析您的自定义模式。

    编辑 :已弃用。这里有一个 new way 做这件事。

        2
  •  0
  •   Vinay Sajip    15 年前

    嗯,你可以这样做,例如:

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

    然后开始工作 configDoc .