protected virtual void Process( XDocument document )
{
var defaultNameAttribute = new XAttribute( "Name", string.Empty );
var defaultValueAttribute = new XAttribute( "Value", string.Empty );
var settings =
document.Descendants( "Section" ).SelectMany(
section => section.Descendants( "Key" ),
( section, setting ) =>
{
var nameAttribute = setting.Attribute( "Name" ) ?? defaultNameAttribute;
var valueAttribute = setting.Attribute( "Value" ) ?? defaultValueAttribute;
var sectionNameAttribute = section.Attribute( "Name" ) ?? defaultNameAttribute;
return new
{
Key = nameAttribute.Value,
valueAttribute.Value,
Name = sectionNameAttribute.Value
};
}
);
IEnumerable<string> sectionList = from section in document.Descendants( "Section" )
select ( section.Attribute( "Name" ) ?? defaultNameAttribute ).Value;
foreach ( string current in sectionList )
{
string sectionName = current; // copy local as we pass it to to linq. We are passing a closure variable.
var items =
settings.Where(
section =>
string.Equals( section.Name, sectionName, StringComparison.OrdinalIgnoreCase ) );
var currentSection = new ConfigurationSection( current );
foreach ( var item in items )
{
currentSection.Set( item.Key, item.Value );
}
Add( currentSection );
}
}