Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8986281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:30:47+00:00 2026-06-15T21:30:47+00:00

I am writing a custom configuration section using the .NET configuration api. I want

  • 0

I am writing a custom configuration section using the .NET configuration api. I want to define a section that can have exactly one of two sub-elements,

e.g.

<MyCustomSection>
    <myItems>
        <myItemType name="1">
            <firstSubTypeConfig />
        </myItemType>
        <myItemType name="2">
            <secondSubTypeConfig />
        </myItemType>
    </myItems>
</MyCustomSection>

Currently, I have the sections defined like:

public class MyCustomSection : ConfigurationSection
{
    public override bool IsReadOnly()
    {
        return false;
    }

    [ConfigurationProperty("myItems")]
    public MyItemsCollection MyItems 
    { 
        get { return (MyItemsCollection)base["myItems"]; }
        set { base["myItems"] = value; }
    }
}

[ConfigurationCollection(typeof(MyItemType), AddItemName="myItemType",
    CollectionType=ConfigurationElementCollectionType.BasicMap)]
public class MyItemsCollection : ConfigurationElementCollection
{
    public override bool IsReadOnly()
    {
        return false;
    }

    public override ConfigurationElementCollectionType CollectionType
    {
        get { return ConfigurationElementCollectionType.BasicMap; }
    }

    protected override string ElementName
    {
        get { return "myItemType"; }
    }

    protected override ConfigurationElement CreateNewElement()
    {
        return new MyItemType();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        return (element as MyItemType).Name;
    }
}


public class MyItemType : ConfigurationElement
{
    public override bool IsReadOnly()
    {
        return false;
    }

    [ConfigurationProperty("name", IsRequired=true)]
    public string Name 
    { 
        get { return (string)base["name"]; }
        set { base["name"] = value; }
    }

    [ConfigurationProperty("firstSubTypeConfig")]
    public FirstSubTypeConfig FirstSubTypeConfig 
    { 
        get { return (FirstSubTypeConfig)base["firstSubTypeConfig"]; }
        set { base["firstSubTypeConfig"] = value; }

    }

    [ConfigurationProperty("secondSubTypeConfig")]
    public SecondSubTypeConfig SecondSubTypeConfig 
    { 
        get { return (SecondSubTypeConfig)base["secondSubTypeConfig"]; }
        set { base["secondSubTypeConfig"] = value; }
    }
}

public class FirstSubTypeConfig : ConfigurationElement
{
    public override bool IsReadOnly()
    {
        return false;
    }
}

public class SecondSubTypeConfig : ConfigurationElement
{
    public override bool IsReadOnly()
    {
        return false;
    }
}

The configuration is also modified and saved programmatically, and currently, saving the
configuration section will add the secondSubTypeConfig element even if I only specify the
firstSubTypeConfig element.

My first thought was to introduce a common base class for FirstSubTypeConfig and SecondSubTypeConfig, but I’m not clear on if or how the configuration api would handle that.

How do I setup mutually exclusive custom config elements?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T21:30:48+00:00Added an answer on June 15, 2026 at 9:30 pm

    I’m not sure this is the “correct” approach, but this works.

    The code as written in the question will load the config file fine. You can check the ElementInformation.IsPresent property to validate that exactly one element is included.

    e.g.

    var custom = (MyCustomSection)ConfigurationManager.GetSection("MyCustomSection");
    foreach (MyItemType itemType in custom.MyItems)
    {
        if (itemType.FirstSubTypeConfig.ElementInformation.IsPresent 
            && itemType.SecondSubTypeConfig.ElementInformation.IsPresent)
        {
            throw new ConfigurationErrorsException("At most one of firstSubTypeConfig or secondSubTypeConfig can be specified in a myItemType element");
        }
        else if (!itemType.FirstSubTypeConfig.ElementInformation.IsPresent
            && !itemType.SecondSubTypeConfig.ElementInformation.Ispresent)
        {
            throw new ConfigurationErrorsException("Either a firstSubTypeConfig or a secondSubTypeConfig element must be specified in a myItemType element");
        }
    }
    

    As for saving the config, it seems that checking for ElementInformation.IsPresent and explicitly setting it to null will prevent the element from being written to the config file. e.g.

    var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    var custom = (MyCustomSection)config.GetSection("MyCustomSection");
    
    //make modifications against the custom variable ...
    
    foreach (MyItemType itemType in custom.MyItems)
    {
        if (!itemType.FirstSubTypeConfig.ElementInformation.IsPresent)
            itemType.FirstSubTypeConfig = null;
        if (!itemType.SecondSubTypeConfig.ElementInformation.IsPresent)
            itemType.SecondSubTypeConfig = null;  
    }
    
    config.Save();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing my own custom configuration section and have a ConfigurationProperty defined in
I am writing a custom configuration section, and I would like to validate a
I am using Spring Framework . While writing custom queries, I am unable check
I'm writing a custom overlay for a Google Map. I have a serious of
I am writing a custom binding handler in Knockout. I want to to pass
I am writing a custom ConfigurationElementCollection for a custom ConfigurationHandler in C#.NET 3.5 and
I'm writing a custom deserializer that will deserialize a list by deserializing each of
I am writing a installer using WiX that will create a website and virtual
I'm using a maven plugin that defines a custom packaging type and lifecycle for
As part of writing custom command (COM-Visible dll with class that implements Interwoven command

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.