2008-12-18 72 views
29

我們在與我們的Io​​C容器類相關的app.config文件中有一個自定義部分。如何在編輯本節的配置文件時獲得智能感知,以及如何擺脫編譯器消息,通知我缺少架構。如何在app.config中爲自定義部分獲取智能感知?

我在這裏發現了這個問題: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>節點內,並獲得註冊建議我在智能感知下拉列表中,還有適當的屬性。

回答

21

XML智能感知不會自動適用於自定義配置部分。

Visual Studio可能在編譯時報告警告,抱怨自定義配置節的屬性未定義。這些警告可能會被忽略。

如果您想爲自定義配置節的XML智能感知支持(或者,如果你只是想警告消失「未找到模式」),下面一行添加到您的DotNetConfig.xsd文件中的第一< XS後立即:架構... >行(通常是DotNetConfig.xsd文件中的第二行)。

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

的DotNetConfig.xsd文件可以在您的Visual Studio 8(或9)安裝目錄中的XML \架構子目錄中找到。

+1

+1。涉及到自定義配置部分時,架構經常被忽略。當你的管理員或合作伙伴必須對app.config或web.config進行更改時,它們非常有用。 – 2008-12-19 14:59:59

+2

我有更好的解決方案。可能需要VS 2010,不知道:) http://stackoverflow.com/questions/1127315/how-do-i-make-an-extension-xsd-for-the-web-app-config-schema/7977168#7977168 – 2011-11-02 08:01:24

4

如果你不想修改你的DotNetConfig.xsd,你可以添加xsd配置「inline」。

在你的情況下添加以下屬性自定義欄目

<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,第二個字符串是模式的位置。

相關問題