Period in XML tags

Studying XAML now, I see XAML tags in Object.Attribute format. For example:

<Deployment.OutOfBrowserSettings> OutOfBrowserSettings ShortName="Hello World" > 

Do not remember how you used to see XML attributes, always saw simple words or possibly a prefix for a namespace column, for example x: application. So, to express the object / attribute relation, as in the previous one, I would expect such a notation as:

 <Deployment> <OutOfBrowserSettings ShortName="Hello World" > 

So my question is: what is the meaning of '.' notation inside XML tags. What is semantics? Is this specific to XAML?

+6
xml xaml
source share
4 answers

To talk a little about what Alex said:

XML does not have a period value in the element name. a , a. , ab and abc are all legal (and unique) element names.

In XAML, there is significant meaning for a period in an element name. Oddly enough, Alex's recommendation quotes, warning you to use period characters in your XML, which is why XAML uses periods: so that XamlReader can determine when it sees first.name that name is a property of the first object. Consequently:

 <ListBox> <ListBox.BorderThickness>2</ListBox.BorderThickness> <ListBox.BorderBrush>Yellow</ListBox.BorderBrush> <TextBox>foo</TextBox> <TextBox>bar</TextBox> <TextBox>baz</TextBox> </ListBox> 

Why can't you just do it?

 <ListBox> <BorderThickness>2</BorderThickness> ... 

There are two reasons. The first is a simple XML design: XML elements can contain multiple elements with the same name. Actually, it is a bad idea to model properties as children, because then you need to ensure uniqueness in your schema or have a rule for what to do with an object property when there are several children with the same name:

 <ListBox> <BorderThickness>2</BorderThickness> <BorderThickness>3</BorderThickness> <BorderThickness>4</BorderThickness> ... 

This is why XAML models properties as attributes that XML must be unique:

 <ListBox BorderThickness='2' BorderBrush='Yellow'... 

(By the way, the problem of using attributes in XAML is: if the properties on the object must be set in a specific order, you should not use the attributes to represent them. It happens that XamlReader reads the attributes in the order in which they appear in the element and assigns them properties in that order, but tools that read and write XML do not guarantee that attribute order is maintained, which means that the person who asked this troubling question can bring grief.)

Another reason is because so many WPF objects are containers of other objects. If XAML allowed elements to represent properties, you would be screwed up if you needed to represent an object that had a property with the same name as the class of the object that it could contain. For example, you could create an ItemsControl that had a Label property, but what happens if you want to save Label objects in the Items property? This ambiguity cannot occur in XAML:

 <MyItemsControl> <MyItemsControl.Label>this is a property of MyItemsControl</MyItemsControl.Label> <Label>this is an item that MyItemsControl contains</Label> </MyItemsControl> 
+11
source share

As far as I know, in XML format . no special meaning. In other words, <ab> is a different and unrelated tag from <a> . Communication in XAML is semantics understood only by the XAML parser.

In a separate note, the snippet in your question is not XAML; This is part of the deployment manifest for the Silverlight application. Again, the semantics of " . " Are understood by the manifest parser, not the XML parser.

+2
source share

Found it today in WPF in action with Visual Studio 2008 from Feldman and Daymon

4.2.3. Using Attached Properties

You may have noticed an interesting thing about the property values ​​in Listing 4.2. Properties, such as Width and Padding, look like regular XML attributes. But the notation of Left and Top properties is slightly different:

 <Button Canvas.Left="40" Canvas.Top="40" > 
Button

has no properties for Left and Top. Unlike Windows Forms, which assumes everything has an explicit location, the working assumption for WPF is that the parent is responsible for hosting each control. You will see this with other types of layouts. In the case of Canvas, each control must have its own explicit location. Since this is a Canvas layout that requires this information, a Canvas layout is required to process this information.

In “classic” XML (that is, XML that can be validated using a schema), you usually need to enter an element around each child element to indicate parent-specific properties, such as list 4.3.

Listing 4.3. Method for setting properties for children (but not supported by WPF)

 <Canvas> <CanvasItem Left = "40" Top="40"> <Button>Button1</Button> </CanvasItem> </Canvas> 

This approach will work, but rather verbose, especially if you have many nested elements. It also implies a hierarchy that does not actually exist. Instead of requiring more verbose XML and making Canvas follow a structure that is probably not needed, XAML introduces a notation that allows properties belonging to the parent object defined on the child object using dot notation:

 <Canvas> <Button Canvas.Left="40" Canvas.Top="50">Button1</Button> </Canvas> 

This should read “When you add a button to the canvas, tell the Canvas that the button should be located left to left and at the top of 40.” These properties are called attached properties because they are bound to the child object to which they belong, even if they belong to the containing control (in this case, Canvas). You will see this designation in XAML for all kinds of properties. You just need to remember that properties with dots in the middle really set the values ​​that are used by the containing control. The best part is that when editing the properties of a control in the property editor, the attached properties are displayed as part of the set of control properties

+2
source share

XML naming conventions are clearly explained, for example. here :

XML elements must follow these rule names:

  • Names may contain letters, numbers, and other characters
  • Names cannot begin with a digit or punctuation character
  • Names cannot begin with the letters xml (or XML, or Xml, etc.)
  • Names cannot contain spaces

You can use any name, without words. reserved.

The URL I gave contains "best practices" recommendations that include

Avoid the "." characters. If you name something "first.name", some software may consider that "name" is a property of the object "first."

But this is just a recommendation (breaking up may make your XML type difficult to use with certain software), as well as using - . Of the recommendations, the only important thing to avoid is use : in XML names (since this contradicts the XML namespace!).

0
source share

All Articles