C# How to create custom section without parent node

447 Views Asked by At

I have worked by below article.

http://www.jokecamp.com/blog/net-custom-configuration-section-collection-and-elements/

I can achieve if I do like what it said. But I want to remove parent node. I want xml to seems like below.

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="jobSection"
      type="MyConfiguration.JobSection, MyConfiguration" />
  </configSections>
  <jobSection>
      <job id="1" name="Job Name A" />
      <job id="2" name="Job Name B" />
  </jobSection>
</configuration>

I get an error Unrecognized element 'job'. if xml is like above. How can I define a custom section by above xml ?

2

There are 2 best solutions below

0
Paolo Tedesco On BEST ANSWER

In your configuration class, you should define the ConfigurationProperty attribute on the collection with an empty string as name and with the IsDefaultCollection property set to true:

[ConfigurationProperty("", IsDefaultCollection = true)]  
public JobsCollection Jobs  
0
Sathyaraj Palanisamy On

I think your post is look similar with below post.Please look the post it may help you.

Unrecognized element "Item" in config file with custom config section