views:

28

answers:

2

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).

A: 

Sorry, something was wrong! The config file is something like this:

    <DatabaseTypeMap>
      <DatabaseType Type="130" Name="Edit" />
      <DatabaseType Type="129" Name="Edit" />
      <DatabaseType Type="3" Name="Count" />
      <DatabaseType Type="135" Name="Date" />
    </DatabaseTypeMap>

an the first code is:

public class DatabaseTypeMapElement : ConfigurationElement {
    [ConfigurationProperty( "DatabaseTypeMap", DefaultValue = "Default", IsRequired = true )]
    public DatabaseTypeElementCollection DatabaseTypeMap {
        get {
            return ( DatabaseTypeElementCollection )this[ "DatabaseTypeMap" ];
        }
        set {
            this[ "DatabaseTypeMap" ] = value;
        }
    }
}
CsillikDavid Janos
This is not an answer to your question - please edit your question and update to the correct details and delete this "answer".
Oded
Aha, an how can I delete it?
CsillikDavid Janos
A: 

Finally I'va found a very good article, if somebody needs:

http://dotnetslackers.com/articles/customconfiguration/Custom_Configuration_Collections.aspx

CsillikDavid Janos