locked
How to serialize back a ConfigurationElement object?

    Question

  • Hello

    In the System.Configuration namespace you have several classes (like ConfigurationElement) which help you to deserialize custom configuration sections into objects.

    Now I have an object from a class which derives from ConfigurationElement and I'd like to serialize it back into its original xml state because I'd like to write it to a trace output (XmlWriterTraceListener). I tried to use the XmlSerializer to serialize the object but this did not work. I got the following error:

    You must implement a default accessor on "MyProperty" because
    it inherits from ICollection.

    Any other ideas how to achieve this? The framework already has a builtin mechanism to deserialize the custom config section. Maybe it's somehow possible to use the same mechanism to serialize the object again?

    Any help is appreciated.

    Kind regards

    Wednesday, January 24, 2007 9:33 PM

Answers

  • Ok, it worked like this:

    ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();

    fileMap.ExeConfigFilename = "MyApp.exe.config";

    System.Configuration.Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);

    object section = config.GetSection("MySection");

    ConfigurationSection configurationSection;

    string xml;

    if(section is ConfigurationSection)

    {

    configurationSection = (ConfigurationSection)section;

    xml = configurationSection.SectionInformation.GetRawXml();

    }

    Thursday, January 25, 2007 11:05 AM

All replies

  • Use the property/method

    SectionInformation.GetRawXml

    of your ConfigurationSection instance.

     

    Thursday, January 25, 2007 9:44 AM
  • Thank you for your suggestion. I tried this:

    object section = ConfigurationManager.GetSection("MySection");
    ConfigurationSection configurationSection;
    string xml;

    if(section is ConfigurationSection)
    {
      configurationSection = (
    ConfigurationSection)section;
      xml = configurationSection.SectionInformation.GetRawXml();
    }

    But the GetRawXml method threw the following exception:

    InvalidOperationException: "This operation does not apply at runtime."

    Any ideas?

    Thursday, January 25, 2007 10:50 AM
  • Ok, it worked like this:

    ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();

    fileMap.ExeConfigFilename = "MyApp.exe.config";

    System.Configuration.Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);

    object section = config.GetSection("MySection");

    ConfigurationSection configurationSection;

    string xml;

    if(section is ConfigurationSection)

    {

    configurationSection = (ConfigurationSection)section;

    xml = configurationSection.SectionInformation.GetRawXml();

    }

    Thursday, January 25, 2007 11:05 AM
  • Exactly! Perfect :)
    Thursday, January 25, 2007 12:22 PM
  • Thanks very useful

    Monday, April 14, 2008 2:02 PM
  • How to deserialize raw XML back to ConfigurationElement?

    Thanks,
    Jay
    Wednesday, October 29, 2008 3:10 PM