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; }
}
}