Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
466 views
in Technique[技术] by (71.8m points)

.net - Enabling Intellisense for Custom Sections in .config Files

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 add a custom configuration section, how can I enable intellisense for my custom settings? I'm sure there must be an easy answer to this, but a cursory Google search didn't give me any help.

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...