C# 如何在 app.config 中获取自定义部分的智能感知?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/378105/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 01:12:54  来源:igfitidea点击:

How do I get intellisense in app.config for a custom section?

c#.netconfigurationapp-configcustom-sections

提问by Lasse V. Karlsen

We have a custom section in my app.config file related to our IoC container class. How can I get intellisense when editing the config file for this section, as well as getting rid of the compiler messages informing me of the missing schema.

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

I found this question here: app.config configSections custom settings can not find schema information, but I don't understand if it applies to my problem or not, and how to use the answer there if it does.

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

I also found this page How to get Intellisense for Web.config and App.config in Visual Studio .NET, but it says to remove the xmlns attribute before running the application. Is that really the only/best way?

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

Here is an example of a simple file:

这是一个简单文件的示例:

<?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>

Basically I would like to be able to type <Rinside the <ServiceContainers>node, and get Registration suggested to me in the intellisense dropdown, as well as the appropriate attributes for it.

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

采纳答案by Steven A. Lowe

XML Intellisense will not automatically work for a custom configuration section.

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

Visual Studio may report warnings on compilation complaining that the attributes of the custom configuration section are not defined. These warnings may be ignored.

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

If you want XML IntelliSense support for a custom configuration section (or if you just want the 'schema not found' warnings to disappear), add the following line to your DotNetConfig.xsd file immediately after the first <xs:schema ...> line (which is typically the second line in the DotNetConfig.xsd file).

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

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

The DotNetConfig.xsd file can be found in your Visual Studio 8 (or 9) installation directory in the Xml\Schemas subdirectory.

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

回答by Juan M. Elosegui

If you don't want to modify your DotNetConfig.xsd you could add the xsd configuration "inline".

如果您不想修改 DotNetConfig.xsd,您可以“内联”添加 xsd 配置。

In your case add the following attributes to the custom section

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

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

         <Registration ....

</ServiceContainers>

This is useful while testing an xsd locally because location_of_your_schemacould be a local path and when you are ready to production change location_of_your_schemato the public url of the xsd file.

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

Note that the xsi:schemaLocationattribute must contain pairs of strings separated by spaces where the first string in each pair is a namespace URI and the second string is the location of a schema.

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