From the course: XAML Fundamentals: Building Powerful UIs for Cross-Platform Applications

Namespace principles in XML

- [Instructor] For this example, I'm using this project XML namespaces and I'm looking at some files in this XML files folder. There's three files here. There's two schema files and a controls XML file. namespaces are common in the programming and markup world. They exist in coding APIs and in XML files. In this video, I'll explore XML namespaces. Later in this chapter, we'll look at the default namespaces for a WPF's XAML file, Then we'll see how Microsoft maps the .net namespaces to the XAML XML namespaces. So let's start with a review or a quick review of the general principles of an XML namespace. They're usually defined in a schema. Now I've defined these schemas in a separate XSD file. So let's start by looking at control schema. When you double click on this in visual studio, it brings up this designer screen. I want to look at the XML. So let's go to view code. So there's three principles for namespaces. A namespace is a container for named items. You can see I have several named items here. I have demo and stack panel and button and I have checkbox and slider. So this is an example of what a control schema might look like. Second, all the name items in the namespace must be unique at the same scope. So I've got demo and stack panel. They're at the same level in here. So if I were to go down and change say the slider and change the name of this to checkbox, I'll get a green squiggle which tells me that checkbox has already been declared. So they have to be unique within scope. And the third principle is that each namespace has a unique name or a URI. So the way we declare the namespace in the schema file is by using target namespace here on the root element. So I say the target namespace for this is this URI. Now to make things unique, typically what happens is that the company that is making the controls owns a domain and they will put the domain on the beginning of their URI so no other company will use the same string. So I'm using a fictitious domain here, schemas.demo.com/controls. That's our first schema file. I also have a second schema file here from a third party. Again, we'll go up here and do view code. So the unique namespace here is schemas.athirdparty.com/animations. So this would be an example of an animation library. So we've got elements that are unique, spinner, rocker, and slider. Now notice that slider is in this animation schema and it's also in the control schema. So they're unique across their namespaces, but they're not unique if we were to use both namespaces in the same XML file. And that's what we're going to do in the next half of this demo is we're going to look at what's in this controls.XML file. First of all, let's close all these tabs. First step is to open this controls.XML file. And I want to start adding some elements here to this root element, the demo root element. So I type in the open angle bracket, and none of my elements are shown here that are in my schema. So there's two things we need to do here. In Visual Studio, we need to tell the Visual Studio editor that those schemas are available. So I can do that by going here and then clicking on these three dots and then clicking on add. And then choosing my two XSD files. Shift + click and click on open. And what you'll see is they were added to the current XML schema set. So now Visual Studio knows about them. So here's the physical file, the controls schema.XSD, and it's showing me that the target namespace that's inside that schema is schemas.demo.com/controls. And here's my third party schema and it's target namespace. So now that I've done that, I can click on okay, and now let's see if I can add an element. Now I still can't add it because I have to bring the namespace into scope in one of our elements. So in this case, I only have one element, the root element. So I'll go up here and I'll type in xmlns=. Visual Studio's looking at that list that we just saw. And it's showing us the available namespaces, target namespaces, and there's my controls. So I'll choose that one. And now when I come down here, now can see all of the elements that were defined in the schemas. So let's add a button. I can set some properties on this. Let's add another one. Let's add a checkbox. For the next one, I want to add the slider. Now remember, the slider was defined in two separate schemas. So let's take a look. As I move over this list, as I get to the slider you'll notice a popup appears. And this tells me that this slider is in the animations namespace and this slider is in the controls namespace. So if I choose this one, it works just the same as the other elements. But if I choose the one in the animations namespace, this one here, it tells the parser that this is in a different schema. So saying this one is available in this schema whereas the slider on line eight is in the animation schema. That's how it can differentiate the unique names. Now what I can do is I can take this namespace and remove it from here and then bring it up to my root element. We'll do that up here. And now the next step is I can't have two namespaces declared as default. So one of these I have to change and put a prefix on it. So I'll put a prefix here, a third party, ATP. So now that that's available, I then can adjust this and say ATP:. And you'll see that when I do that, it shows rocker, slider, and spinner. Those are the ones that are defined in the third-party schema. So now you have a better understanding of XML namespace declaration and a namespace prefixes. Next up is a tour of the XAML namespaces.

Contents