如何在app.config中获取自定义部分的intellisense?

时间:2008-12-18 15:06:23

标签: c# .net configuration app-config custom-sections

我的app.config文件中有一个与我们的Io​​C容器类相关的自定义部分。在编辑本节的配置文件时,如何获取智能感知,以及摆脱编译器消息,告知我缺少的模式。

我在这里找到了这个问题:app.config configSections custom settings can not find schema information,但我不明白它是否适用于我的问题,以及如果有问题,如何使用答案。

我也找到了这个页面How to get Intellisense for Web.config and App.config in Visual Studio .NET,但它说在运行应用程序之前删除了xmlns属性。这真的是唯一/最好的方式吗?

以下是一个简单文件的示例:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="ServiceContainers"
        type="LVK.IoC.RegistrationsSectionHandler, LVK"/>
  </configSections>
  <ServiceContainers>
    <Registration type="DatabaseConnection" class="DatabaseConnection">
      <Parameter name="connectionString" type="System.String"
          value="TYPE=MSSQL2000;SERVER=localhost;DATABASE=db"/>
    </Registration>
  </ServiceContainers>
</configuration>

基本上我希望能够在<R节点内输入<ServiceContainers>,并在intellisense下拉列表中向我建议注册,以及相应的属性。

2 个答案:

答案 0 :(得分:21)

XML Intellisense不会自动适用于自定义配置部分。

Visual Studio可能会报告有关编译的警告,抱怨未定义自定义配置部分的属性。这些警告可能会被忽略。

如果您希望XML IntelliSense支持自定义配置部分(或者您只是希望“找不到架构”警告消失),请在第一个&lt; xs:schema之后立即将以下行添加到DotNetConfig.xsd文件中...&GT; line(通常是DotNetConfig.xsd文件中的第二行)。

<xs:include schemaLocation="YOUR_DIRECTORY\namespace.assemblyname.xsd"/>

DotNetConfig.xsd文件可以在Xml \ Schemas子目录中的Visual Studio 8(或9)安装目录中找到。

答案 1 :(得分:4)

如果您不想修改您的DotNetConfig.xsd,您可以添加xsd配置&#34; inline&#34;。

在您的情况下,将以下属性添加到自定义部分

<ServiceContainers xmlns="your_xmlns"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="your_xmlns location_of_your_schema">

         <Registration ....

</ServiceContainers>

这在本地测试xsd时很有用,因为 location_of_your_schema 可以是本地路径,当您准备好生产时,将 location_of_your_schema 更改为xsd文件的公共URL。

请注意, xsi:schemaLocation 属性必须包含由空格分隔的字符串对,其中每对中的第一个字符串是名称空间URI,第二个字符串是模式的位置。

相关问题