Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Configuring parser behavior

Ricardo Amores Hernández edited this page · 2 revisions

Since the INI format isn't standardized, Ini Parser version 2.0 introduces a simpler way to customize the way an INI file is parsed, and thus providing an easy way to handle all the different format variations.

Using an IniParserConfiguration object you can redefine how the parser will handle special items when parsing an ini file. For example you can redefine the character used as comment so instead of using the character ';' (default) you could set instead that lines starting with the '#' character are comments.

You can also define how the parser should treat errors (e.g. throw exceptions or just return a null value), or how liberal or conservative should it be when parsing files with "strange" formats.

When creating an instance of an IniDataParser you can pass a configuration object. The configuration object follows the . IIniParserConfiguration interface. If you don't provide an instance of an a default one will be used.

You can access and modify the configuration for the parser using the Configuration property in the IniDataParser instance. See the parse_ini_string_with_custom_configuration test for an example.

If you find yourself using a custom configuration in a lot of places, it will be better to create a custom configuration object with the behavior you need. Just create a derived class from BaseIniParserConfiguration and set your custom property's values in the constructor. This is the approach the DefaultIniParserConfiguration actually uses.

This is a list of the configuration options as defined in the IIniParserConfiguration interface specification:

type Name Description
bool AllowDuplicateKeys If duplicate keys are find in a section and this is false the parser will stop with an error. If it is true the value of the duplicate key will be the last value asigned to the key. Defaults to false.
bool AllowDuplicateSections If set to false and a duplicate section is found, the parser will stop with an error. If set to true, duplicated sections are allowed in the file, but only one SectionData element will be created in the IniData.Sections collection, effectively merging all the keys in the duplicated sections of the same name. Defaults to false.
bool AllowKeysWithoutSection  Allows having keys in the file that don't belong to any section. i.e. allows defining keys before defining a section. If set to false and keys without a section are defined, the parser will stop with an error. Defaults to true.
string  AssigmentSpacer Sets the string around KeyValuesAssignmentChar Defaults to the string ' '
Regex CommentRegex Regular expression used to match a comment string
char CommentChar Sets the char that defines the start of a comment. A comment spans from the comment character to the end of the line. Defaults to character '#'
char KeyValueAssigmentChar Sets the char that defines a value assigned to a key Defaults to character '='
bool OverrideDuplicateKeys Only applies if AllowDuplicateKeys is true. If set to true when the parser finds a duplicate key, it overwrites the previous value, so the key will always contain the value of the last key readed in the file. If set to false the first readed value is preserved, so the key will always contain the value of the first key read in the file. Defaults to false.
char SectionEndChar Sets the char that defines the end of a section name. Defaults to character ']'
Regex SectionRegex Regular expression used to match a section string
char  SectionStartChar Sets the char that defines the start of a section name. Defaults to character '['
bool SkipInvalidLines If set to true, when the parser finds an invalid line, it just skips it instead of throwing a ParseException or returning null when parsing (if exceptions are disabled because the property ThrowExceptionsOnError is set to false). Defaults to false
bool ThrowExceptionsOnError If true the parser will thrown an exception if an error is found. If false the parser will just stop execution and return a null value as result . Defaults to true.

If you need a fine grainer customization for the parsing, you can create a derived class from IniDataParser and overwrite some methods. Please see the page Modifying Parser

Something went wrong with that request. Please try again.