I have the following code
var section = new CustomConfigurationSection();
section.SectionInformation.Type = "System.Configuration.NameValueFileSectionHandler";
section.SectionInformation.SetRawXml(sectionXml);
configuration.Sections.Add(sectionName, section);
last line of which throws:
ConfigurationErrorsException An error occurred executing the configuration section handler for monitor.
with the inner exception:
Unrecognized element 'screens'. (line 1) (line 1)
Definition of CustomConfigurationSection:
public class CustomConfigurationSection: ConfigurationSection
{
public CustomConfigurationSection()
{
}
}
configuration is an instance of a custom class, which has a property named Sections, that have the type 'ConfigurationSectionCollection'.
And the incoming xml in sectionXml is:
<monitor>
<screens>
<screen>
<regions>
<region>
<labelCoordinates />
<startupApplication>Internet</startupApplication>
<color />
<width>426</width>
<height>266</height>
<x1>0</x1>
<x2>0</x2>
<y1>0</y1>
<y2>0</y2>
</region>
</regions>
<height>800</height>
<width>1280</width>
</screen>
<screen>
<regions />
<height>0</height>
<width>0</width>
</screen>
</screens>
</monitor>
How can I get this to work?
After looking at your example one reason I can see that it won't work is you are using the NameValueFileSectionHandler. If I remember correctly this only allows the following syntax:
Based on the xml you are wanting to use you probably need to fully implement the config section classes. So you would have something like the following:
This would then use xml like the following:
The main point is that I am using a collection (which you have in your example, actually a couple of them). Within that collection is an object that I am representing. So to get it to parse the xml you have you would have to make a section handler to match what you are using.
Based on my example you would probably want to change your xml to something along the line of:
You could then use classes similar to my example. Although you could probably get what you are wanting but you will need to use other configuration items to get it to work that way.
Hope that helps.