1

I'm trying to read and deserialize the App.config XML to object, but i'm getting the following error:

An exception of type 'System.InvalidOperationException' occurred in System.Xml.dll but was not handled in user code

my code:

 AppConfigXML myclass = null;
 string configpath = "mypath";

 XmlSerializer serializer = new XmlSerializer(typeof(AppConfigXML));

 StreamReader reader = new StreamReader(configpath);
 myclass = (AppConfigXML)serializer.Deserialize(reader); //<- Error to deserialize

This is my App.config class to serialize and deserialize:

public class AppConfigXML
{   

        [XmlRoot(ElementName = "section")]
        public class Section
        {
            [XmlAttribute(AttributeName = "name")]
            public string Name { get; set; }

            [XmlAttribute(AttributeName = "type")]
            public string Type { get; set; }

            [XmlAttribute(AttributeName = "requirePermission")]
            public string RequirePermission { get; set; }
        }

        [XmlRoot(ElementName = "configSections")]
        public class ConfigSections
        {
            [XmlElement(ElementName = "section")]
            public Section Section { get; set; }
        }

        [XmlRoot(ElementName = "supportedRuntime")]
        public class SupportedRuntime
        {
            [XmlAttribute(AttributeName = "version")]
            public string Version { get; set; }

            [XmlAttribute(AttributeName = "sku")]
            public string Sku { get; set; }
        }

        [XmlRoot(ElementName = "startup")]
        public class Startup
        {
            [XmlElement(ElementName = "supportedRuntime")]
            public SupportedRuntime SupportedRuntime { get; set; }
        }

        [XmlRoot(ElementName = "parameter")]
        public class Parameter
        {
            [XmlAttribute(AttributeName = "value")]
            public string Value { get; set; }
        }

        [XmlRoot(ElementName = "parameters")]
        public class Parameters
        {
            [XmlElement(ElementName = "parameter")]
            public Parameter Parameter { get; set; }
        }

        [XmlRoot(ElementName = "defaultConnectionFactory")]
        public class DefaultConnectionFactory
        {
            [XmlElement(ElementName = "parameters")]
            public Parameters Parameters { get; set; }

            [XmlAttribute(AttributeName = "type")]
            public string Type { get; set; }
        }

        [XmlRoot(ElementName = "provider")]
        public class Provider
        {
            [XmlAttribute(AttributeName = "invariantName")]
            public string InvariantName { get; set; }

            [XmlAttribute(AttributeName = "type")]
            public string Type { get; set; }
        }

        [XmlRoot(ElementName = "providers")]
        public class Providers
        {
            [XmlElement(ElementName = "provider")]
            public Provider Provider { get; set; }
        }

        [XmlRoot(ElementName = "entityFramework")]
        public class EntityFramework
        {
            [XmlElement(ElementName = "defaultConnectionFactory")]
            public DefaultConnectionFactory DefaultConnectionFactory { get; set; }

            [XmlElement(ElementName = "providers")]
            public Providers Providers { get; set; }
        }

        [XmlRoot(ElementName = "add")]
        public class Add
        {
            [XmlAttribute(AttributeName = "name")]
            public string Name { get; set; }

            [XmlAttribute(AttributeName = "connectionString")]
            public string ConnectionString { get; set; }

            [XmlAttribute(AttributeName = "providerName")]
            public string ProviderName { get; set; }

            [XmlAttribute(AttributeName = "key")]
            public string Key { get; set; }

            [XmlAttribute(AttributeName = "value")]
            public string Value { get; set; }
        }

        [XmlRoot(ElementName = "connectionStrings")]
        public class ConnectionStrings
        {
            [XmlElement(ElementName = "add")]
            public List<Add> Add { get; set; }
        }

        [XmlRoot(ElementName = "appSettings")]
        public class AppSettings
        {
            [XmlElement(ElementName = "add")]
            public List<Add> Add { get; set; }
        }

        [XmlRoot(ElementName = "configuration")]
        public class Configuration
        {
            [XmlElement(ElementName = "configSections")]
            public ConfigSections ConfigSections { get; set; }

            [XmlElement(ElementName = "startup")]
            public Startup Startup { get; set; }

            [XmlElement(ElementName = "entityFramework")]
            public EntityFramework EntityFramework { get; set; }

            [XmlElement(ElementName = "connectionStrings")]
            public ConnectionStrings ConnectionStrings { get; set; }

            [XmlElement(ElementName = "appSettings")]
            public AppSettings AppSettings { get; set; }
        }
}
Mario Guadagnin
  • 476
  • 7
  • 17
  • 1
    have you looked for any working examples that are on Stackoverflow as well as the internet..? http://stackoverflow.com/questions/4123590/serialize-an-object-to-xml – MethodMan Sep 09 '16 at 18:40
  • 2
    "This is my App.config class" Where? I can see here only many classes – Roman Marusyk Sep 09 '16 at 18:46
  • Megatron - This is the full class – Mario Guadagnin Sep 09 '16 at 19:03
  • 2
    1) Can you share the XML file that generates the error? 2) What is the full `ToString()` output of the exception including the exception type, message, traceback, and **`InnerException`** if any? The inner exception often gives useful details for `XmlSerializer` exceptions. – dbc Sep 09 '16 at 19:08
  • 2
    Your type `AppConfigXML` class has no [properties](https://msdn.microsoft.com/en-us/library/x9fsa0sw.aspx) or [fields](https://msdn.microsoft.com/en-us/library/ms173118.aspx)! It only has some [nested types](https://msdn.microsoft.com/en-us/library/ms173120.aspx). Thus there is nothing to serialize. – dbc Sep 09 '16 at 19:09
  • 2
    Here's a [fiddle](https://dotnetfiddle.net/O1OBey) demonstrating that your `AppConfigXML` has no actual properties or fields. – dbc Sep 09 '16 at 19:19
  • 1
    @MarioGuadanhim I see it now. Thanks that updated the question – Roman Marusyk Sep 09 '16 at 19:58

0 Answers0