.net - How do I handle unknown attributes in a configuration file -


in our application, have custom configuration save , read. our problem this: product evolves, configuration properties no longer needed, or renamed, or whatever. files written previous versions of our product have these properties in them, when these files read exception thrown (configurationerrorsexception).

is there way catch exception unknown property ignored?

here example of mean. if try read following file:

<?xml version="1.0" encoding="utf-8"?> <configuration>     <configsections>         <section name="mysettings" type="config.mysettingsconfiguration, config, version=0.0.0.0, culture=neutral, publickeytoken=null" allowlocation="true" allowdefinition="everywhere" allowexedefinition="machinetolocaluser" overridemodedefault="allow" restartonexternalchanges="true" requirepermission="true" />     </configsections>     <mysettings>         <myelement bogus=""/>     </mysettings> </configuration> 

and "bogus" not defined property of "myelement", exception thrown. able catch exception , ignore "bogus" (or not have exception thrown @ all).

thanks!

jab

usually fact proves problem, means deployment practices aren't good. should not have residual, deprecated configuration values in config files of deployed applications. prepare configuration transforms , deploy software including configuration files , problem go away.

however, can prevent exception being thrown , continue parsing.

from creating configuration sections – p3.net:

configuration elements/sections expose 2 overridable methods (ondeserializeunrecognizedattribute , ondeserializeunrecognizedelement) called if parse finds unknown element/attribute during parsing. these methods can used support simple dynamic parsing.

for unknown attributes method gets name , value parsed. if method returns true subsystem assumes attribute handled otherwise exception thrown.

so override configurationelement.ondeserializeunrecognizedattribute method (string, string) in mysettings : configurationelement class , return true when know attribute called deprecated.

so this:

public class mysettings : configurationelement {     private string[] _deprecatedattributes = new[] { "foo", "bar" };      protected override bool ondeserializeunrecognizedattribute(string attribute, string value)     {         if (_deprecatedattributes.contains(attribute))         {             return true;         }          return base.ondeserializeunrecognizedattribute(attribute, value);     } } 

you need check , can't return true attributes, break validation attributes invalid , never valid.


Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -