Skip to content

Unable to ignore namespace and attributes with namespace prefixes on the config section. #11

@mkvideodj

Description

@mkvideodj

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions