Home

The Structure of an XML Tag

 

Empty Tags

We mentioned that, unlike HTML, every XML tag must be closed. We also saw that the value of a tag was specified on the right side of the right angle bracket of the start tag. In some cases, you will create a tag that doesn't have a value or, may be for some reason, you don't provide a value to it. Here is an example:

<dinner></dinner>

This type of tag is called an empty tag. Since there is no value in it, you may not need to provide an end tag but it still must be closed. Although this writing is allowed, an alternative is to close the start tag itself. To do this, between the tag name and the right angle bracket, type an empty space followed by a forward slash. Based on this, the above line can be written as follows:

<dinner />

Both produce the same result or accomplish the same role.

White Spaces

In the above example, we typed various items on the same line. If you are creating a long XML document, although creating various items on the same line is acceptable, this technique can make it (very) difficult to read. One way you can solve this problem is to separate tags with empty spaces. Here is an example:

<title>The Distinguished Gentleman</title> <director>Jonathan Lynn</director> <length>112 Minutes</length>

Yet a better solution consists of typing each item on its own line. This would make the document easier to read. Here is an example:

<title>The Distinguished Gentleman</title>
<director>Jonathan Lynn</director>
<length>112 Minutes</length>

All these are possible and acceptable because the XML parser doesn't consider the empty spaces or end of line. Therefore, to make your code easier to read, you can use empty spaces, carriage-return-line-feed combinations, or tabs inserted in various sections. All these are referred to as white spaces.

Nesting Tags

Most XML files contain more than one tag. We saw that a tag must have a starting point and a tag must be closed. One tag can be included in another tag: this is referred to as nesting. A tag that is created inside of another tag is said to be nested. A tag that contains another tag is said to be nesting. Consider the following example:

<Smile>Please smile to the camera</Smile>
<English>Welcome to our XML Class</English>
<French>Bienvenue à notre Classe XML</French>

In this example, you may want the English tag to be nested in the Smile tag. To nest one tag inside of another, you must type the nested tag before the end-tag of the nesting tag. For example, if you want to nest the English tag in the Smile tag, you must type the whole English tag before the </Smile> end tag. Here is an example:

<Smile>Please smile to the camera<English>Welcome to our XML Class</English></Smile>

To make this code easier to read, you can use white spaces as follows:

<smile>Please smile to the camera
<English>Welcome to our XML Class</English>
</smile>

When a tag is nested, it must also be closed before its nesting tag is closed. Based on this rule, the following code is not valid:

<Smile>Please smile to the camera
<English>Welcome to our XML Class
</Smile>
</English>

The rule broken here is that the English tag that is nested in the the Smile tag is not closed inside the Smile tag but outside.

Practical Learning Practical Learning: Creating XML

  1. To apply the concept of nesting XML tags, change the Parts.xml file as follows:
     
    <?xml version="1.0" encoding="utf-8" ?>
    <Parts>
    	<Part>
    		<CarYear>2005</CarYear>
    		<Make>Acura</Make>
    		<Model>MDX 3.5 4WD</Model>
    		<PartNumber>293749</PartNumber>
    		<PartName>Air Filter</PartName>
    		<UnitPrice>16.85</UnitPrice>
    	</Part>
    	<Part>
    		<CarYear>2002</CarYear>
    		<Make>Audi</Make>
    		<Model>A4 Quattro</Model>
    		<PartNumber>283759</PartNumber>
    		<PartName>Clutch Release Bearing</PartName>
    		<UnitPrice>55.50</UnitPrice>
    	</Part>
    </Parts>
  2. Access the Program.cs file and change the call to the XmlDocument.Load() method as follows:
     
    using System;
    using System.Xml;
    
    namespace CollegeParkAutoParts1
    {
        class Program
        {
            static int Main(string[] args)
            {
                XmlDocument docXML = new XmlDocument();
    
                docXML.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
    			   "<Employees><Employee>" +
                               "<EmplNumber>48-705</EmplNumber>" +
                               "<FirstName>John</FirstName>" +
                               "<LastName>Cranston</LastName>" +
                               "<HourlySalary>16.48</HourlySalary>" +
                               "</Employee><Employee>" +
                               "<EmplNumber>22-688</EmplNumber>" +
                               "<FirstName>Annie</FirstName>" +
                               "<LastName>Loskar</LastName>" +
                               "<HourlySalary>12.50</HourlySalary>" +
                               "</Employee><Employee>" +
                               "<EmplNumber>85-246</EmplNumber>" +
                               "<FirstName>Bernie</FirstName>" +
                               "<LastName>Christo</LastName>" +
                               "<HourlySalary>22.52</HourlySalary>" +
                               "</Employee><Employee></Employees>"  );
                
                docXML.Save("Employees.xml");
                return 0;
            }
        }
    }
  3. Execute the application
  4. Close the DOS window
  5. Open Windows Explorer and display the contents of the folder of the current project.
    Notice the presence of the parts.xml and the Employees.xml files
  6. Double-click each file to preview it in the browser
 

Previous Copyright © 2006-2016, FunctionX, Inc. Next