XML Attributes |
|
#region Using directives using System; using System.Collections.Generic; using System.Text; #endregion 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); Console.ReadLine(); } } } 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 |
<?xml version="1.0" encoding="utf-8"?> <logininfo> <credential username="belld" password="qwyIYw58" /> <credential username="democracy" password="2k!2hk3W" /> <credential username="autocrate" password="$*@#ywEy" /> <credential username="progress" password="36%y68F$" /> </logininfo> |
private void Form1_Load(object sender, System.EventArgs e) { this.dataSet1.ReadXml("credentials.xml"); this.dataGrid1.DataSource = this.dataSet1; this.dataGrid1.DataMember = "credential"; this.dataGrid1.CaptionText= "Login Credentials"; } |
|
||
Home | Copyright © 2004-2010 FunctionX, Inc. | |
|