-
Notifications
You must be signed in to change notification settings - Fork 8
Description
Hi there,
Thanks for this library - I've rolled my own configuration classes before and this is much less boilerplate code to write - I much prefer your library.
However, I would like to use an schema XSD file alongside my configuration section so that I can benefit from intellisense in Visual Studio and document the configuration settings.
To do this I added an XSD describing my custom config section and decorated the my config section that will be processed by AutoConfig with an xsi:noNamespaceSchemaLocation attribute and xmlns:xsi - something like this:
However the namespace declaration attributes are picked up in the automatic mapping so I errors because AutoConfig cannot map these attributes to my config class.
I cloned the repo and debugged and found that MappingFactory.CreateMapping() includes namespace declarations - so I ingored the xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" declaration by adding a where clause to ingnore it :
var attributeList = sectionElement.Attributes().Where(x => !x.IsNamespaceDeclaration).ToList();
Of course, it still tries to match the xsi:noNamespaceSchemaLocation attribute. To get around this I added a NoNamespaceSchemaLocation to my config interface and defined it as optional with AutoConfig.WhenMapping<> but the code in MappingFactory.TakeAttribute is not namespace aware.
So I changed MappingFactory.TakeAttribute() to use LocalName instead:
static XAttribute TakeAttribute(ICollection attributeList, string name)
{
return TakeSingleMatching(attributeList, attr => attr.Name.LocalName == name,
count => string.Format("Found {0} attributes with name '{1}' but only expected to find one.", count, name));
}
This works for my purposes, but you may wish to add optional logic to ignore attributes for specific namespaces and namespace prefixes as a better solution. If you could configure via AutoConfig.WhenMapping() to ignore the a given namespace that would be perfect.
Something like:
AutoConfig.WhenMapping(mapping =>
{
mapping.IgnoreNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance")
});
Then you could change element/attribute mapping to see if the namespace is in the ignore list and ignore any that are not part of the actual config for the project.
Thanks,
Darryl