Enabling Intellisense for Custom Sections in .config Files

后端 未结 3 1536
夕颜
夕颜 2020-12-12 18:50

When editing .NET config files (app.config, web.config, etc) in Visual Studio, I get Visual Studio\'s intellisense to guide me when choosing my application\'s settings. If I

相关标签:
3条回答
  • 2020-12-12 19:20

    As the other answers say, you need to provide an XML Schema document for your custom configuration section. There's no need to add the .xsd schema file to some global directory; you can reference it directly from your custom section in the App.config file:

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

    The xmlns attribute is merely there to set a default namespace, so that you don't need to set it on your customSection element and all of its child elements. (However, do not place the xmlns attribute on the <configuration> element!)

    The customSection.xsd contains the schema that will be used by IntelliSense, for example:

    <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>
    
    0 讨论(0)
  • 2020-12-12 19:25

    You need to create an XSD file for your custom settings and copy it to the schema directory of your visual Studio install. For 2005, this is: %ProgramFiles%\Microsoft Visual Studio 8\XML\Schemas

    Here some information on this. http://blogs.msdn.com/astebner/archive/2005/12/07/501466.aspx

    0 讨论(0)
  • 2020-12-12 19:37

    If you do not want to modify your Visual Studio files or copy anything into the Visual Studio folder, you can add the .xsd file to your project, open your .config file and select Schemas in the Properties window (click the […] icon):

    Screenshot of Visual Studio showing where to find and change the "Schemas" property of your <code>.config</code> file

    0 讨论(0)
提交回复
热议问题