Can anybody tell me, how can I read this from a config file?
<DatabaseTypeMap>
<DatabaseType Type="130" Name="Edit" />
<DatabaseType Type="129" Name="Edit" />
<DatabaseType Type="3" Name="Count" />
<DatabaseType Type="135" Name="Date" />
</DatabaseTypeMap>
I use that:
public class DatabaseTypeMapElement : ConfigurationElement {
[ConfigurationProperty( "DatabaseTypeMap", DefaultValue = "Default", IsRequired = true )]
public DatabaseTypeElementCollection DatabaseTypeMap {
get {
return ( DatabaseTypeElementCollection )this[ "DatabaseTypeMap" ];
}
set {
this[ "DatabaseTypeMap" ] = value;
}
}
}
public class DatabaseTypeElementCollection : ConfigurationElementCollection {
protected override ConfigurationElement CreateNewElement() {
return new DatabaseTypeElement();
}
protected override object GetElementKey( ConfigurationElement element ) {
return ( ( DatabaseTypeElement )element ).Type;
}
protected override bool OnDeserializeUnrecognizedElement( string elementName, System.Xml.XmlReader reader ) {
if( elementName=="DatabaseType" ) {
DatabaseTypeElement newElement = new DatabaseTypeElement();
newElement.DeserializeElement( reader );
Add( newElement );
}
return true;
}
internal void Add( DatabaseTypeElement newElement ) {
BaseAdd( newElement, true );
}
internal void Remove( DatabaseTypeElement element ) {
BaseRemove( element.Name );
}
internal void Remove( string elementKey ) {
BaseRemove( elementKey );
}
}
public class DatabaseTypeElement : DeserializableConfigurationElement {
[ConfigurationProperty( "Type", IsRequired = true )]
[StringValidator( InvalidCharacters = "~!@#$%^&*()[]{}/;'\"|\\", MinLength = 1, MaxLength = 60 )]
public String Type {
get {
return ( String )this[ "Type" ];
}
set {
this[ "Type" ] = value;
}
}
[ConfigurationProperty( "Name", IsRequired = true )]
[StringValidator( InvalidCharacters = "~!@#$%^&*()[]{}/;'\"|\\", MinLength = 1, MaxLength = 60 )]
public String Name {
get { return ( String )this[ "Name" ]; }
set { this[ "Name" ] = value; }
}
}
But I always get: The type initializer for 'XYService' threw an exception (yes, it is a service).