Dot-Net

為 .config 文件中的自定義部分啟用 Intellisense

  • April 13, 2009

在 Visual Studio 中編輯 .NET 配置文件(app.config、web.config 等)時,我得到 Visual Studio 的智能感知來指導我選擇應用程序的設置。如果我添加自定義配置部分,如何為我的自定義設置啟用智能感知?我相信這個問題一定有一個簡單的答案,但粗略的Google搜尋並沒有給我任何幫助。

謝謝!

如果您不想修改 Visual Studio 文件或將任何內容複製到 Visual Studio 文件夾中,可以將.xsd文件添加到項目中,打開文件並在“屬性.config”視窗中選擇“架構” (點擊圖示):[…]

Visual Studio 的螢幕截圖,顯示在何處查找和更改 <code>.config</code> 文件的“架構”屬性

正如其他答案所說,您需要為自定義配置部分提供 XML Schema 文件。無需將.xsd架構文件添加到某個全域目錄;您可以直接從App.config文件中的自定義部分引用它:

<configuration>

 <!-- make the custom section known to .NET's configuration manager -->
 <configSections>
   <section name="customSection" type="..." />
 </configSections>

 <!-- your custom section -->
 <customSection xmlns="http://tempuri.org/customSection.xsd"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xsi:noNamespaceSchemaLocation="customSection.xsd">
   ...
 </customSection>

<configuration>

xmlns屬性僅用於設置預設命名空間,因此您無需在customSection元素及其所有子元素上設置它。(但是,不要將xmlns屬性放在<configuration>元素上!)

customSection.xsd包含 IntelliSense 將使用的架構,例如:

<xs:schema id="customSectionSchema"
          targetNamespace="http://tempuri.org/customSection.xsd"
          elementFormDefault="qualified"
          xmlns="http://tempuri.org/customSection.xsd"
          xmlns:mstns="http://tempuri.org/customSection.xsd"
          xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <xs:element name="customSection">
   ...
 </xs:element>
</xs:schema>

引用自:https://stackoverflow.com/questions/742905