Live Chat Icon For mobile
Live Chat Icon

WPF FAQ - XML Namespaces

Find answers for the most frequently asked questions
Expand All Collapse All

WPF defines a CLR attribute that is consumed by XAML processors in order to map multiple CLR namespaces to a single XML namespace. The ‘xlmns’ Definition Attribute is placed at the assembly level in the source code that produces the assembly. The WPF assembly source code uses this attribute to map the various common namespaces such as System.Windows and System.Windows.Controls to the http://schemas.microsoft.com/winfx/2006/xaml/presentation namespace.

The ’xmlns’ Definition Attribute takes two parameters: the XML namespace name and the CLR namespace name. More than one ’xmlns’ Definition Attribute can exist to map multiple CLR namespaces to the same XML namespace. Once mapped, members of those namespaces can also be referenced without full qualification if desired, by providing the appropriate using statement in the partial-class code-behind page. For more details, see ’xmlns Definition Attribute’.

Permalink

In order for your custom type converter to be used as the acting type converter for a custom class, you must apply the .NET Framework “TypeConverter” attribute to your class definition. The Converter Type ’Name’ that you specify through the attribute must be the type name of your custom type converter. With this attribute applied, when an XAML processor handles values where the property type uses your custom class type, it can input strings and return object instances.

You can also provide a type converter on a per-property basis. Instead of applying a .NET Framework “TypeConverter” attribute to the class definition, apply it to a property definition (the main definition, not the get / set implementations within it). The type of the property must match the type that is processed by your custom type converter. With this attribute applied, when an XAML processor handles values of that property, it can process input strings and return object instances. The per-property type converter technique is particularly useful if you choose to use a property type from Microsoft .NET Framework or from some other library where you cannot control the class definition and cannot apply a “TypeConverter” attribute there.

Permalink

There is one more type of namespace declaration that can be used to associate namespace identifiers with element names. This is known as a default namespace declaration which uses the following syntax.

xmlns=’<namespace identifier>’

Notice that there is no prefix. When a default namespace declaration is used on an element, all unqualified element names within it’s scope are automatically associated with the specified namespace identifier.

Default namespace declarations, however, have absolutely no effect on attributes. The only way to associate an attribute with a namespace identifier is through a prefix.

Consider the following example.

[XAML]

<d:student  xmlns:d=’http://www.develop.com/student’
     xmlns=’urn:foo’ id=’3235329’
>
  <name>Jeff Smith</name>
  <language xmlns=’’>C#</language>
  <rating>35</rating>
</d:student>

Here, ‘student’ is from the http://www.develop.com/student namespace while ‘name’ and ‘rating’ are from the default namespace urn:foo. The ’id’ attribute doesn’t belong to a namespace since attributes aren’t automatically associated with the default namespace identifier.

This example also illustrates that you can undeclare a default namespace by simply setting the default namespace identifier back to the empty string as shown in the language element (remember you cannot do this with prefix declarations). As a result, the language element also doesn’t belong to a namespace.

The syntax for default namespaces was designed for convenience but they tend to cause more confusion than their worth. The confusion typically stems from the fact that elements and attributes are treated differently and it’s not immediately apparent that nested elements are being assigned the default namespace identifier. Nevertheless, in the end, choosing between prefixes and default namespaces is mostly a matter of style, except when attributes come into play.

Permalink

Share with

Couldn't find the FAQs you're looking for?

Please submit your question and answer.