C# App.config:自定义配置嵌套节

C# App.config:自定义配置嵌套节,c#,configuration,app-config,sections,C#,Configuration,App Config,Sections,我已经找到并尝试将其用于我自己的实现 我已按如下方式设置App.config: <configSections> <section name="DocumentationSettings" type="ConfigHandler.DocumentationSettings,Settings"/> </configSections> <DocumentationSettings> <DocumentationSections>

我已经找到并尝试将其用于我自己的实现

我已按如下方式设置App.config:

<configSections>
  <section name="DocumentationSettings" type="ConfigHandler.DocumentationSettings,Settings"/>
</configSections>

<DocumentationSettings>
  <DocumentationSections>
    <DocumentationSection Id="AAA">
      <SectionDescription Value="SectionDescriptionAAA"/>
    </DocumentationSection>
    <DocumentationSection Id="BBB">
      <SectionDescription Value="SectionDescriptionBBB"/>
    </DocumentationSection>
    <DocumentationSection Id="CCC">
      <SectionDescription Value="SectionDescriptionCCC"/>
    </DocumentationSection>
  </DocumentationSections>
</DocumentationSettings>
section.Properties.Id
section.SectionDescription.Properties.Value
public DocumentationSectionConfigElement this[int index]
<section>
  <subSections>
    <subSection name="111">
      <Description Value="AAA"></Description>
      <Headers>
        <Header type="Main">
         <Properties>
           <Property name="Property1"/>
           <Property name="Property2"/>
           <Property name="Property3"/>
         </Properties>
        </Header>
      </Headers>
    </subSection>
  </subSections>
</section>
第一个“Console.WriteLine”非常好用

因此,我有以下问题和实施相关问题:

  • 我在第二个“Console.WriteLine”上遇到错误,错误:无法识别的属性“Value”。因为“DocumentationSectionConfigElement”类公开了属性访问,所以我把“public SectionDescription SectionDescription”放进去了,但我可能错了,我尝试先把它放进“DocumentationSectionCollection”中,但我不知道如何在那里实现它,对我来说,“DocumentationSectionCollection”似乎只实现了“Collection”逻辑

  • 我想访问“字段”,如下所示:

    <configSections>
      <section name="DocumentationSettings" type="ConfigHandler.DocumentationSettings,Settings"/>
    </configSections>
    
    <DocumentationSettings>
      <DocumentationSections>
        <DocumentationSection Id="AAA">
          <SectionDescription Value="SectionDescriptionAAA"/>
        </DocumentationSection>
        <DocumentationSection Id="BBB">
          <SectionDescription Value="SectionDescriptionBBB"/>
        </DocumentationSection>
        <DocumentationSection Id="CCC">
          <SectionDescription Value="SectionDescriptionCCC"/>
        </DocumentationSection>
      </DocumentationSections>
    </DocumentationSettings>
    
    section.Properties.Id
    section.SectionDescription.Properties.Value
    
    public DocumentationSectionConfigElement this[int index]
    
    <section>
      <subSections>
        <subSection name="111">
          <Description Value="AAA"></Description>
          <Headers>
            <Header type="Main">
             <Properties>
               <Property name="Property1"/>
               <Property name="Property2"/>
               <Property name="Property3"/>
             </Properties>
            </Header>
          </Headers>
        </subSection>
      </subSections>
    </section>
    
    第.Id节 section.SectionDescription.Value

  • 或者像这样:

    <configSections>
      <section name="DocumentationSettings" type="ConfigHandler.DocumentationSettings,Settings"/>
    </configSections>
    
    <DocumentationSettings>
      <DocumentationSections>
        <DocumentationSection Id="AAA">
          <SectionDescription Value="SectionDescriptionAAA"/>
        </DocumentationSection>
        <DocumentationSection Id="BBB">
          <SectionDescription Value="SectionDescriptionBBB"/>
        </DocumentationSection>
        <DocumentationSection Id="CCC">
          <SectionDescription Value="SectionDescriptionCCC"/>
        </DocumentationSection>
      </DocumentationSections>
    </DocumentationSettings>
    
    section.Properties.Id
    section.SectionDescription.Properties.Value
    
    public DocumentationSectionConfigElement this[int index]
    
    <section>
      <subSections>
        <subSection name="111">
          <Description Value="AAA"></Description>
          <Headers>
            <Header type="Main">
             <Properties>
               <Property name="Property1"/>
               <Property name="Property2"/>
               <Property name="Property3"/>
             </Properties>
            </Header>
          </Headers>
        </subSection>
      </subSections>
    </section>
    
    我看到“Collection”允许直接使用索引器方法使用这些属性,如下所示:

    <configSections>
      <section name="DocumentationSettings" type="ConfigHandler.DocumentationSettings,Settings"/>
    </configSections>
    
    <DocumentationSettings>
      <DocumentationSections>
        <DocumentationSection Id="AAA">
          <SectionDescription Value="SectionDescriptionAAA"/>
        </DocumentationSection>
        <DocumentationSection Id="BBB">
          <SectionDescription Value="SectionDescriptionBBB"/>
        </DocumentationSection>
        <DocumentationSection Id="CCC">
          <SectionDescription Value="SectionDescriptionCCC"/>
        </DocumentationSection>
      </DocumentationSections>
    </DocumentationSettings>
    
    section.Properties.Id
    section.SectionDescription.Properties.Value
    
    public DocumentationSectionConfigElement this[int index]
    
    <section>
      <subSections>
        <subSection name="111">
          <Description Value="AAA"></Description>
          <Headers>
            <Header type="Main">
             <Properties>
               <Property name="Property1"/>
               <Property name="Property2"/>
               <Property name="Property3"/>
             </Properties>
            </Header>
          </Headers>
        </subSection>
      </subSections>
    </section>
    
    但我不能在“SectionDescription”类上实现indexer方法,因为它是单个节而不是集合,所以当我访问字段时,这个“Properties”名称仍然存在

  • 我需要添加什么才能在这些配置对象上使用LINQ? 我的意思是:

    <configSections>
      <section name="DocumentationSettings" type="ConfigHandler.DocumentationSettings,Settings"/>
    </configSections>
    
    <DocumentationSettings>
      <DocumentationSections>
        <DocumentationSection Id="AAA">
          <SectionDescription Value="SectionDescriptionAAA"/>
        </DocumentationSection>
        <DocumentationSection Id="BBB">
          <SectionDescription Value="SectionDescriptionBBB"/>
        </DocumentationSection>
        <DocumentationSection Id="CCC">
          <SectionDescription Value="SectionDescriptionCCC"/>
        </DocumentationSection>
      </DocumentationSections>
    </DocumentationSettings>
    
    section.Properties.Id
    section.SectionDescription.Properties.Value
    
    public DocumentationSectionConfigElement this[int index]
    
    <section>
      <subSections>
        <subSection name="111">
          <Description Value="AAA"></Description>
          <Headers>
            <Header type="Main">
             <Properties>
               <Property name="Property1"/>
               <Property name="Property2"/>
               <Property name="Property3"/>
             </Properties>
            </Header>
          </Headers>
        </subSection>
      </subSections>
    </section>
    
    (documentationSettings.DocumentationSections.Sections)。选择(x=>x.Id)

  • 有没有什么真正好的复杂XML结构配置处理程序的例子?从我发现的结构中,大部分都是这样的简单结构:

  • 但没有任何这样复杂结构的例子:

    <configSections>
      <section name="DocumentationSettings" type="ConfigHandler.DocumentationSettings,Settings"/>
    </configSections>
    
    <DocumentationSettings>
      <DocumentationSections>
        <DocumentationSection Id="AAA">
          <SectionDescription Value="SectionDescriptionAAA"/>
        </DocumentationSection>
        <DocumentationSection Id="BBB">
          <SectionDescription Value="SectionDescriptionBBB"/>
        </DocumentationSection>
        <DocumentationSection Id="CCC">
          <SectionDescription Value="SectionDescriptionCCC"/>
        </DocumentationSection>
      </DocumentationSections>
    </DocumentationSettings>
    
    section.Properties.Id
    section.SectionDescription.Properties.Value
    
    public DocumentationSectionConfigElement this[int index]
    
    <section>
      <subSections>
        <subSection name="111">
          <Description Value="AAA"></Description>
          <Headers>
            <Header type="Main">
             <Properties>
               <Property name="Property1"/>
               <Property name="Property2"/>
               <Property name="Property3"/>
             </Properties>
            </Header>
          </Headers>
        </subSection>
      </subSections>
    </section>
    

    由于这里没有活动,我将尝试逐一回答我的问题:

  • 发现它描述了很多事情,并显示了一些复杂的自定义配置实现示例

  • 我发现,这正是我想要的——它采用XML结构并生成自定义配置代码