Intellisense for custom config section problem with namespaces
Asked Answered
P

1

5

I have just rolled a custom configuration section, created an accompanying schema document for Intellisense and added it to the Web.config's Schemas property as per Michael Stum's answer to another similar question.

Unfortunately, and possibly due to me creating the XSD by hand with limited knowledge, the Intellisense relies on an xmlns attribute pointing to my XSD file's namespace being present in the custom config element. However, when running the project I get an Unrecognized attribute 'xmlns'. Note that attribute names are case-sensitive error.

I could probably just modify my XSD file to define the xmlns attribute for that element, however I am wondering if this is just a bandaid fix to a larger problem. I must confess I don't have a very good understanding of XML namespaces so this might be an oppportunity to set me straight on a few things.

Here is the attributes for my XSD file's root xs:schema element:

<xs:schema id="awesomeConfig"
           targetNamespace="http://awesome.com/schemas"
           xmlns="http://awesome.com/schemas"
           elementFormDefault="qualified"
           xmlns:xs="http://www.w3.org/2001/XMLSchema">
    ...
</xs:schema>

And on creating the element in the Web.config file, Visual Studio 2008 automatically appends:

<awesomeConfig xmlns="http://awesome.com/schemas"></awesomeConfig>

So have I misunderstood the meaning of the xs:schema attributes at all, or is the proper solution as simple as it seems?

Phidias answered 7/6, 2010 at 0:3 Comment(0)
K
5

Your schema will need to omit the targetNamespace attribute. In effect, this will put the schema contents into the default namespace.

That's necessary because the .NET configuration system has never permitted elements to be in a namespace.

Keeler answered 7/6, 2010 at 0:6 Comment(2)
Would I be right in saying that the consequence of this is that I must avoid naming clashes? If so, how would I implement the standard collection elements of add/remove/clear?Phidias
Those standard elements are not a naming clash if you use the same syntax. Also, the schema will be relative to the parent elements. If you make the root of your schema be your configuration section name, then there will be no name conflicts, as long as your schema is strictly hierarchical. Make the add/remove/clear elements child elements of their parent instead of global elements referenced later.Keeler

© 2022 - 2024 — McMap. All rights reserved.