XML Attributes



One of XML strengths is its ability to describe data with various options, using simple or complex elements. Although an element can have a value as large as a long paragraph of text, an element can have only one value. There are cases where you would like the same element to be equipped with, or be able to provide, various values that can also be easily distinguished or separate. To provide various types of information to an XML element, you can use one or more attributes


Creating an Attribute

In C#, we are used to creating classes. Imagine that you want to create one for employees. Such a class would appear as follows:

#region Using directives

using System;
using System.Collections.Generic;
using System.Text;


namespace CSharpLessons
    class CEmployeeRecord
        public string Username;
        public string Password;
        public Double Salary;
        public char   MaritalStatus;

    class Program
        static void Main(string[] args)
            CEmployeeRecord emplRecord = new CEmployeeRecord();

            emplRecord.Username = "kallar";
            emplRecord.Password = "7hd47D89";
            emplRecord.Salary   = 20.12;
            emplRecord.MaritalStatus = 'D';

            Console.WriteLine("Username:       {0}", emplRecord.Username);
            Console.WriteLine("Password:       {0}", emplRecord.Password);
            Console.WriteLine("Marital Status: {0}", emplRecord.MaritalStatus);
            Console.WriteLine("Hourly  Salary: {0}", emplRecord.Salary);


This would produce:

Username:       kallar
Password:       7hd47D89
Marital Status: D
Hourly  Salary: 20.12

The members of such a class are said to describe the class. When you instantiate the class (when you declare a variable of that class), you can provide value for one or more member variables. Another instance of the class can have different values.

In XML, a tag is like a variable of a C++ class, except that you don't have to create the class but you must create the tag. Inside of the start tag, you can provide one or more attributes that mimic the member variables of a class.

An attribute is created in the start tag using the formula:

<tag Attribute_Name="Value">Element_Value</tag>

Like the tag, the name of an attribute is up to you. On the right side of the attribute, type its value in double-quotes. The end tag doesn't need any information about any attribute. It is only used to close the tag. Here is an example of a tag that uses an attribute:

<salary status="Full Time">22.05</salary >

In this example, status="Full Time" is called an attribute of the salary element.

One of the good features of an attribute is that it can carry the same type of value as that of an XML tag. Therefore, using an attribute, you can omit giving a value to a tag. For example, instead of creating the following tag with its value:

<movie>Coming to America</movie>

You can use an attribute to carry the value of the tag. Here is an example:

<movie title="Coming to America"></movie>

In this case, you can still provide another or new value for the tag.

You can create more than one attribute in a tag. To do this, separate them with an empty space. Here is an example:

<movie title="Coming to America" director="John Landis" length="116 min">Nile Rodgers</movie>

If you create a tag that uniquely contains attributes without a formal value, you can omit the end tag. In this case, you can close the start tag as you would do for an empty tag. Here is an example:

<movie title="Coming to America" />


Practical Learning: Creating XML Attributes

  1. If you want to apply an example, start Microsoft Visual C# and create a new Windows Application named Attributes1
    The New Project dialog box of Microsoft Visual C# 2005 Express Edition Beta
  2. To create a new XML file, on the main menu, click Project -> Add New Item...
  3. In the Templates section, click XML File (.xml)
  4. In the Name text box, type credentials and press Enter
  5. Complete the file as follows:
    <?xml version="1.0" encoding="utf-8"?>
    	<credential username="belld" password="qwyIYw58" />
    	<credential username="democracy" password="2k!2hk3W" />
    	<credential username="autocrate" password="$*@#ywEy" />
    	<credential username="progress" password="36%y68F$" />
  6. Save this credentials.xml file in your bin\Debug folder
  7. From the Windows Forms section of the Toolbox, click a DataGrid and click the form
  8. From the Data section of the Toolbox, click a DataSet and click the form
  9. In the Add Dataset dialog box, click the second radio button
  10. Click OK
  11. Double-click an unoccupied area on the form and implement the Load event as follows:
    private void Form1_Load(object sender, System.EventArgs e)
    			this.dataGrid1.DataSource = this.dataSet1;
    			this.dataGrid1.DataMember = "credential";
    			this.dataGrid1.CaptionText= "Login Credentials";
  12. Test the application
  13. Close the form

Home Copyright © 2004-2010 FunctionX, Inc.