XML and .NET Namespaces Collide in XAML
The term namespace is overloaded: the same word has different meanings in different contexts. (Or does that mean it's polymorphic? Hmm ) In .NET, a namespace is a place where classes and other namespaces are logically contained-- a bit like folders in a file system. And, also like folders, the namespace defines the context in which a class name must be unique. You can have a MyClass in ThisNamespace and a different MyClass in ThatNamespace, but you can't have two different MyClass classes in the same namespace. Beyond defining the context of unique naming .NET namespaces provide a logical organization to the tens of thousands of classes in the .NET Framework.
But in XML, namespace are different In fact, it's best to completely forget about .NET namespaces when you think about XML namespaces. An XML namespace is a unique name given to the schema being used in an XML file. A schema is the "grammar and vocabulary" of the XML file--it defines what tags and attributes are valid and how they should be organized into elements and sub-elements. If Microsoft had created XML in the beginning, schema names might have been implemented as GUID's because that's essentially all they are, a unique series of characters used to name the schema, assuring that it never conflicts with the name of another schema. But XML doesn't use GUID's. Instead it uses URL's (Technically it uses the more broadly defined URI, but that's not essential here).
When you see a URL, you immediately think of a Web address. But XML namespace URL's don't necessarily point to a real location on the Internet. They are not to be understood as addresses, even though they look like one. When you begin to create your own schema, you select a domain that you own and then add any combination of "virtual folders" after the domain name to specifically identify your schema. Since you own the domain, no one else will use that domain when creating a different schema. And you are responsible for seeing that no two schemas you create have the same "virtual folders" after the domain name. For example, if I own edgequest.com, I can name my schema:
Once you understand that .NET namespaces and XML namespaces are completely different concepts, you might be surprised when you first come to XAML. XAML combines the concept of XML namespaces with .NET namespaces.
XAML is usually discussed in the context of Windows Presentation Foundation (WPF). It is the schema that is used to define what a WPF form looks like, what controls it contains and how they are arranged. But XAML is more than a user interface schema. It is actually a way of representing and using .NET classes in XML. This approach makes it easy to quickly define objects and set their properties while writing little or no code in C# or VB. For many applications (including UI definition), a XAML file is much more efficient and easily maintainable than code written in traditional languages.
So if you are going to use XAML to work with .NET
classes, you must specify which .NET namespaces you want to
work with--much like you do with a using in C# or an Imports
in VB. XAML uses XML namespaces to do this. Because, in
essence, what you are doing by identifying the .NET
namespace is specifying the schema for this XAML file.
Syntax to access and use the
namespace, for example, might look like this:
<WINDOW XMLNS:COLL="clr-namesapce:System.Collections;assembly=mscorlib"> <COLL:ARRAYLIST> <SYSTEM.STRING>Brave New World</SYSTEM.STRING> <SYSTEM.STRING>The Scarlet Letter</SYSTEM.STRING> <SYSTEM.STRING>Huckleberry Finn</SYSTEM.STRING> <SYSTEM.STRING>Foundation</SYSTEM.STRING> </COLL:ARRAYLIST> </WINDOW>
As you can see, the XML namespace begins with clr- namespace: followed by the fully-qualified .NET namespace. The assembly clause at the end wouldn't actually be necessary in this case, it's only required if the namespace is not in a well-known assembly. Once the namespace is identified, you are free to use classes it contains, like the ArrayList, in this case.
In WPF, you can identify the typical WPF namespaces and get a whole slew of namespaces available to you automatically. But if you want to refer to a different well- known assembly or you want to use classes from your own projects, XAML's special use of the XML namespace syntax makes it possible.