Introduction to the Extensible Markup Language
Introduction to the Extensible Markup Language
The Extensible Markup Language
Introduction to XML
The Extensible Markup Language, or XML, is a technique of using a document, such as a text file, to describe information in a special way and make that information available to whatever and whoever can take advantage of it. The created document is not a program. It is just a text-based document.
XML is very flexible, it can be used in regular applications, in databases, in web-based systems, in communication applications, in computer networks, in scientific applications, etc. To make sure that XML can be universally used, it is standardized by the W3C (http://www.w3c.org) organization. XML is released through an XML Recommendation document with a version.
To support XML, the .NET Framework provides many classes. These classes closely follow all aspects of XML standards. To create an application that uses XML files, you have various options.
An XML document is a text-based document made of units called entities. These entities are spread on various lines of the document as you judge them necessary and as we will learn. XML has strict rules as to how the contents of the document should or must be structured.
After an XML document has been created and is available, in order to use it, you need a program that can read, analyze, and interpret it. This program is called a parser. The most popular parser used in Microsoft Windows is named MSXML, published by Microsoft.
Practical Learning: Introducing XML
(Name: btnStoreInventory Text: Store Inventory
A Markup
A markup is an instruction that defines XML. The fundamental formula of a markup is:
<tag>
The left angle bracket "<" and the right angle bracket ">" are required. Inside these symbols, you type a word or a group of words of your choice, using regular characters of the English alphabet and sometimes non-readable characters such as ?, !, or [. The combination of a left angle bracket "<", the right angle bracket ">", and what is inside those symbols is called a markup. There are various types of markups we will learn.
The Document Type Declaration (DTD)
As mentioned above, XML is released as a version. Because there can be various versions, the first line that can be processed in an XML file must specify the version of XML you are using. At the time of this writing, the widely supported version of XML is 1.0. When creating an XML file, you should (should in 1.0 but must in 1.1) specify what version your file uses, especially if you are using a version higher than 1.0. For this reason, an XML file should start (again, must, in 1.1), in the top section, with a line known as an XML declaration. It starts with <?xml version=, followed by the version you are using, assigned as a string, and followed by ?>. An example of such a line is:
<?xml version="1.0"?>
By default, an XML file created using Microsoft Visual Studio specifies the version as 1.0. Under the XML declaration line, you can then create the necessary tags of the XML file.
Encoding Declaration
As mentioned already, XML tags are created using characters of the alphabet and conform to the ISO standard. This is known as the encoding declaration. For example, most of the characters used in the US English language are known as ASCII. These characters use a combination of 7 bits to create a symbol. Such an encoding is specified as UTF-8. There are other standards such as UTF-16 (for wide, 2-Byte, characters).
To specify the encoding you are using, type encoding followed by the encoding scheme you are using, which must be assigned as a string. The encoding is specified in the first line. Here is an example:
<?xml version="1.0" encoding="utf-8"?>
Creating an XML File
Due to the high level of support of XML in the .NET Framework, there are various ways you can create and use an XML file. An XML file is first of all a normal text-based document. It uses the extension .xml. Therefore, however you create it, you must specify that extension.
Practical Learning: Starting an XML Document
Writing XML Code
Introduction to the Document Object Model
To implement XML, the .NET Framework provides the System.Xml namespace. When you create an XML file, there are standard rules you must follow in order to have a valid document. The standards for an XML file are defined by the W3C Document Object Model (DOM). To support these standards, the System.Xml namespace provides a class named XmlDocument. This class allows you to create an XML document, to populate it with the desired contents, and to perform many other related operations on the contents of the file.
To start an XML file, if you are writing your code in a class, include the System.Xml namespace in the top section of the file. Declare an XmlDocument variable and initialize it. Here is an example:
using System; using System.Xml; public class Exercise { public static int Main(string[] args) { XmlDocument xdStudents = new XmlDocument(); return 1000; } }
Practical Learning: Introducing the Document Object Model
using System; using System.IO; using System.Xml; using System.Windows.Forms; namespace DepartmentStore6 { public partial class StoreInventory : Form { public StoreInventory() { InitializeComponent(); } private void btnStoreInventory_Click(object sender, EventArgs e) { XmlDocument xdStoreItems = new XmlDocument(); } } }
Writing XML Code Using XmlDocument
To let you create XML code using XmlDocument, the class is equipped with a method named LoadXml. Its syntax is:
public virtual void LoadXml(string xml);
This method takes a string as argument. The XmlDocument.LoadXml() method doesn't create an XML file, it only allows you to provide or create XML code. The code can be created as argument. You can also first declare and initialize a string variable with the XML code, then pass it as argument to the XmlDocument.LoadXml() method.
Writing XML Code Using the Code Editor
In the next sections, we will see how to create an XML file. After creating the file and displaying it in the Code Editor, you can start editing it. The Code Editor is equipped to assist you with various options. Whenever you start typing, the editor would display one or more options to you:
When different options are presented to you, you can press the arrow keys to select an option and press Enter. You can also use the mouse to click an option.
Saving an XML File
Introduction
Probably the most common way to create an XML file in Microsoft Windows consists of using Notepad or any other text editor. After opening the text editor, you can enter the necessary lines of code. After creating the file, you must save it. As mentioned when studying file processing, you should save your XML files in the App_Data folder of your project. If you are using a text editor like Notepad, make sure you add the .xml extension to the file.
Saving a DOM Object
If you call the XmlDocument.LoadXml() method, only the XML code is created, not the file. To let you create the file, the XmlDocument class is equipped with an overloaded method named Save. This method is provided in four versions. One of the versions takes a string as argument. The syntax of this method is:
public virtual void Save(string filename);
The argument must be a valid filename and must include the .xml extension. If you pass a string without backslashes, the file would be created in the same folder as the current project.
Opening an XML File
Introduction
Whether you created an XML file or someone else did, you can open it easily to view its contents. The easiest way to open an XML file is to use a text editor, such as Notepad. Because the Code Editor has a friendlier appearance and it is available to you, it is a better candidate. To open an XML file, on the main menu, you can click File -> Open File..., locate the file, and click Open.
An XML File in a Browser
Another way you can display an XML file is in a browser. To do this, if you see the file in a file utility such as Windows Explorer or in My Documents, you can double-click it.
Programmatically Opening an XML File Using the DOM
At times, you will need to programmatically access an XML file. To support this operation, the XmlDocument class provides a method named Load. It is available in various versions. One of the syntaxes used by this method is:
public virtual void Load(string filename);
This version takes as argument the name or path of the file. Here is an example of calling it:
using System;
using System.Xml;
public class Program
{
static int Main(string[] args)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("Videos.xml");
return 0;
}
}
If the compiler doesn't find the file, it would throw a FileNotFoundException exception. For this reason, it is cautious to first check that the file exists before opening it. You can do this by calling the System.IO.File.Exists() method. Here is an example:
using System; using System.IO; using System.Xml; public class Program { static int Main(string[] args) { XmlDocument xmlDoc = new XmlDocument(); string strFilename = "Videos.xml"; if (File.Exists(strFilename)) xmlDoc.Load(strFilename); else Console.WriteLine("The file {0} could not be located", strFilename); return 0; } }
An alternative is to handle an exception yourself.
You can also use a Stream-based object to identify the file. Once the object is ready, you can use the following version of the Load() method to open it:
public virtual void Load(Stream inStream);
This method expects a Stream type of object, such as a FileStream variable. Here is an example of calling it:
using System; using System.IO; using System.Xml; public class Program { static int Main(string[] args) { string strFilename = "Videos.xml"; XmlDocument xmlDoc = new XmlDocument(); FileStream fsVideos = null; if (File.Exists(strFilename)) { fsVideos = new FileStream(strFilename, FileMode.Open, FileAccess.Read); xmlDoc.Load(fsVideos); } else Console.WriteLine("The file {0} could not be located", strFilename); return 0; } }
Programmatically Reading an XML File
Many of the XML files you encounter will have been created by someone else. Still, because it is primarily a text document, you are expected to be able to read any XML file and figure out its content. As mentioned already, you can open an XML file using a text editor such as Notepad. After opening the file, you can start by checking the document declaration, then move to other items.
Another way you can explore an XML file consists of programmatically reading it. This is also referred to as parsing (the parser parses the document). To support reading an XML file, the .NET Framework provides the abstract XmlReader class as the ancestor of classes that can read an XML file. One of the classes derived from XmlReader is called XmlTextReader. The XmlTextReader class provides the ability to read the file from the left to the right sides and from the top to the bottom sections. This class has very important characteristics you must remember:
To programmatically read an XML file, you can start by declaring a variable of type XmlTextReader using one of its constructors, including the default. To specify the file you want to open and read, you can use the constructor whose syntax is :
public XmlTextReader(string url);
When using this method, pass the name of the file or its path as argument. Here is an example:
using System;
using System.IO;
using System.Xml;
public class Program
{
static int Main(string[] args)
{
string strFilename = "Videos.xml";
if (File.Exists(strFilename))
XmlTextReader rdrXml = new XmlTextReader(strFilename);
else
Console.WriteLine("The file {0} could not be located",
strFilename);
return 0;
}
}
You can also identify a file using a Stream-based object. Once the object is ready, you can pass it to the following constructor of the class:
public XmlTextReader(Stream input);
Here is an example:
using System; using System.IO; using System.Xml; public class Program { static int Main(string[] args) { string strFilename = "Videos.xml"; FileStream fsVideos = new FileStream(strFilename, FileMode.Open, FileAccess.Read); if (File.Exists(strFilename)) XmlTextReader rdrXml = new XmlTextReader(fsVideos); else Console.WriteLine("The file {0} could not be located", strFilename); fsVideos.Close(); return 0; } }
To actually read the file, the XmlTextReader class is equipped with a method named Read(). Its syntax is:
public override bool Read();
As you may suspect, this method only tells you that it successfully read an item. It doesn't tell you what it read. As stated already, the XmlTextReader scans a file in a left-right-top-down direction. When it has read something, it returns true. If it didn't or couldn't read something, it returns false. Therefore, you can call it to read an item.
After it has read an item, you can call the the XmlTextReader.Read() method again to move to the next item. If there is a next item, it reads it and returns true. But, if there is no next item, the XmlTextReader.Read() method would not be able to read it and it would return false. In other words, you can ask the XmlTextReader.Read() method to continuously read the items as long as it returns true. Once it cannot read an item, you can ask it to stop. To perform this exercise, you can use a loop (while or do...while). This would be done as follows:
sing System; using System.IO; using System.Xml; public class Program { static int Main(string[] args) { string strFilename = "Videos.xml"; FileStream fsVideos = new FileStream(strFilename, FileMode.Open, FileAccess.Read); XmlTextReader rdrXml = new XmlTextReader(fsVideos ); do { // Read an item and return true // Continue reading as long as ... } while (rdrXml.Read() == true); // ... as long as Read() returns true // Once Read() returns false, STOP!!! fsVideos.Close(); return 0; } }
To identify what was read, the XmlTextReader provides methods appropriate for the different types of item that an XML file can contain. Starting in the next lesson, we will review the types of items of a file.
XML Well-Formed
Earlier, we mentioned that XML worked through markups. A simple markup is made of a tag created between the left angle bracket "<" and the right angle bracket ">". Just creating a markup is not particularly significant. You must give it meaning. To do this, you can type a number, a date, a character, or a string on the right side of the right angle bracket ">" symbol. The text on the right side of ">" is referred to as the item's text. It is also called a value.
After specifying the value of the markup, you must close it: this is a rule not enforced in HTML but must be respected in XML to make it "well-formed". To close a tag, use the same formula of creating a tag with the left angle bracket "<", the tag, and the right angle bracket ">" except that, between < and the tag, you must type a forward slash. The formula to use is:
<tag>some value</tag>
The item on the left side of the "some value" string, in this case <tag>, is called the opening or start-tag. The item on the right side of the "some value" string, in this case </tag>, is called the closing or end-tag. Like<tag> is a markup, </tag> also is called a markup.
With XML, you create your own tags with custom names. This means that a typical XML file is made of various items. Here is an example:
<title>The Distinguished Gentleman</title> <director>Jonathan Lynn</director><length>112 Minutes</length>
XML and HTML belong to the same family of languages. In fact, they primarily follow the same synx when it comes to their tags or markup. Still, HTML is quite flexible and allows you to violate some rules without penalty. XML is not forgiving with its rules. At the end, when writing your HTML code, you should strive to follow strict rules.
Tag Names
When creating your tags, there are various rules you must observe with regards to their names. Unlike HTML, XML is very restrictive with its rules. For example, unlike HTML but like C/C++/C#/Java/F#, XML is case-sensitive. This means that CASE, Case, and case are three different words. Therefore, from now on, you must pay close attention to what you write inside of the < and the > delimiters.
Besides case sensitivity, there are some rules you must observe when naming the tags of your markups:
In our lessons, here are the rules we will apply:
In future sections, we will learn that, with some markups, you can include non-readable characters between the angle brackets. In fact, you will need to pay close attention to the symbols you type in a markup. We will also see how some characters have special meaning.
Practical Learning: Creating an XML Tag
<?xml version="1.0" encoding="utf-8" ?>
<store-items></store-items>
The Root
Every XML document must have one particular tag that, either is the only tag in the file, or acts as the parent of all the other tags of the same document. This tag is called the root. Here is an example of a file that has only one tag:
<rectangle>A rectangle is a shape with 4 sides and 4 straight angles</rectangle>
This would produce:
If there are more than one tag in the XML file, one of them must serve as the parent or root. Otherwise, you would receive an error. Based on this rule, the following XML code is not valid:
<rectangle>A rectangle is a shape with 4 sides and 4 straight angles</rectangle> <square>A square is a rectangle whose 4 sides are equal</square>
This would produce:
To correct this type of error, you can change one of the existing tags to act as the root. In the following example, the <rectangle> tag acts as the parent:
<rectangle>A rectangle is a shape with 4 sides and 4 straight angles <square>A square is a rectangle whose 4 sides are equal</square></rectangle>
This would produce:
Alternatively, you can create a tag that acts as the parent for the other tags. In the following example, the <geometry> tag acts as the parent of the <rectangle> and of the <square> tags:
<geometry><rectangle>A rectangle is a shape with 4 sides and 4 straight angles </rectangle><square>A square is a rectangle whose 4 sides are equal</square></geometry>
This would produce:
As mentioned already, a good XML file should have a Document Type Declaration:
<?xml version="1.0" encoding="utf-8"?><geometry><rectangle>A rectangle
is a shape with 4 sides and 4 straight angles</rectangle><square>A
square is a rectangle whose 4 sides are equal</square></geometry>
To give you access to the root of an XML file, the XmlDocument class is equipped with the DocumentElement property.
The Structure of an XML Tag
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, 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.
Once you have decided on the structure of your XML file, we saw that you can create it in memory by calling the XmlDocument.LoadXml() method. For example, the following XML code:
<?xml version="1.0" encoding="utf-8"?> <musiccollection> <album> <shelfnumber>FJ-7264</shelfnumber> <title>Symphony-Bantu</title> <artist>Vincent Nguini</artist> <copyrightyear>1994</copyrightyear> <publisher>Mesa Records</publisher> </album> <album> <shelfnumber>MR-2947</shelfnumber> <title>None</title> <artist>Debbie Gibson</artist> <copyrightyear>1990</copyrightyear> <publisher>Atlantic</publisher> </album> </musiccollection>
can be created in memory as follows:
public partial class Exercise : Form { public Exercise() { InitializeComponent(); } private void btnDocumentClick(object sender, EventArgs e) { System.Xml.XmlDocument xdMusic = new System.Xml.XmlDocument(); xdMusic.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "<musiccollection><album>" + "<shelfnumber>FJ-7264</shelfnumber>" + "<title>Symphony-Bantu</title>" + "<artist>Vincent Nguini</artist>" + "<copyrightyear>1994</copyrightyear>" + "<publisher>Mesa Records</publisher></album>" + "<album><shelfnumber>MR-2947</shelfnumber>" + "<title>None</title><artist>Debbie Gibson</artist>" + "<copyrightyear>1990</copyrightyear>" + "<publisher>Atlantic</publisher>" + "</album></musiccollection>"); } }
Notice that the whole XML code can be created as one line of text and the code would be valid.
Practical Learning: Completing an XML File
<?xml version="1.0" encoding="utf-8" ?>
<store-items>
<store-item>
<item-name>Blue Striped Dress</item-name>
<size>Medium</size>
<price>148.85</price>
</store-item>
<store-item>
<item-name>Khaki Pants</item-name>
<size>38-32</size>
<price>42.50</price>
</store-item>
<store-item>
<item-name>Sahara Sand Skirt</item-name>
<size>6</size>
<price>60</price>
</store-item>
</store-items>
An XML Node
Introduction to XML Nodes
Consider the following example of an XML file named Videos.xml:
<?xml version="1.0" encoding="utf-8" ?> <Videos> <Video> <Title>The Distinguished Gentleman</Title> <Director>Jonathan Lynn</Director> <Length>112 Minutes</Length> <Format>DVD</Format> <Rating>R</Rating> </Video> <Video> <Title>Her Alibi</Title> <Director>Bruce Beresford</Director> <Length>94 Mins</Length> <Format>DVD</Format> <Rating>PG-13</Rating> </Video> <Video> <Title>Chalte Chalte</Title> <Director>Aziz Mirza</Director> <Length>145 Mins</Length> <Format>DVD</Format> <Rating>N/R</Rating> </Video> </Videos>
An XML file appears as an upside-down tree: it has a root (in this case <Videos>), it can have branches (in this case <Video>), and it can have leaves (an example in this case is <Title>). As we have seen so far, all these objects are created using the same technique: a tag with a name (such as <Title>) and an optional value. Based on their similarities, each of these objects is called a node.
To support nodes of an XML file, the .NET Framework provides a class named XmlNode:
public abstract class XmlNode : ICloneable, IEnumerable, IXPathNavigable
The XmlNode class is the ancestor to all types of nodes. XmlNode is an abstract class without a constructor. Based on this, to get a node, you must have an object that would produce one and you can only retrieve a node from an (existing) object.
Introduction to Node Types
An XML document can contain various types of nodes. The categories or possible types of nodes are identified by an enumeration named XmlNodeType. If you use an XmlTextReader object to scan a document, when calling its Read() mathod, the class has a property named NodeType that allows you to identify the node that was read. NodeType is a read-only property of type XmlNodeType and it is declared as follows:
public override XmlNodeType NodeType { get; }
Therefore, when calling the XmlTextReader.Read() method, you can continuously check the value of the XmlTextReader.NodeType property to find out what type of node was just read, and then you can take an appropriate action.
Elements Fundamentals
Introduction
An element in an XML document is an object that begins with a start-tag, may contain a value, and may terminate with an end-tag. Based on this, the combination of a start-tag, the value, and the end-tag is called an element. An element can be more than that but for now, we will consider that an element is primarily characterized by a name and possibly a value.
To support XML elements, the System.Xml namespace provides a class named XmlElement. To access an XML element, you can declare a variable of type XmlElement but the main purpose of this class is to get an element from a DOM object. For this reason, the XmlElement class doesn't have a constructor you can use. Instead, and as we will learn, the other classes have methods that produce an XmlElement element you can manipulate as necessary.
We already know that every XML file must have a root and we mentioned that you could use the XmlDocument.DocumentElement property to access it. This property is of type XmlElement. To access it, you can declare an XmlElement variable and assign it this property. Here is an example:
File: videos.xml
<?xml version="1.0" encoding="utf-8"?> <videos> <video> <title>The Distinguished Gentleman</title> <director>Jonathan Lynn</director> <length>112 Minutes</length> <format>DVD</format> <rating>R</rating> </video> <video> <title>Her Alibi</title> <director>Bruce Beresford</director> <length>94 Mins</length> <format>DVD</format> <rating>PG-13</rating> </video> <video> <title>Chalte Chalte</title> <director>Aziz Mirza</director> <length>145 Mins</length> <format>DVD</format> <rating>N/R</rating> </video> </videos>
Here is an example of accessing the XML file:
using System.IO; using System.Xml; public class Exercise { public static int Main(string[] args) { string strVideosFile = "Exercise.xml"; XmlDocument xdVideos = new XmlDocument(); FileInfo fiVideos = new FileInfo(strVideosFile); if(fiVideos.Exists == true) { xdVideos.Load(strVideosFile); XmlElement xeVideo = xdVideos.DocumentElement; } return 20; } }
An XML element is represented in the XmlNodeType enumeration as the Element member. When using the XmlTextReader.Read() method, to find out if the item being read is an element, you can check whether the member of the current XmlNodeType is Element. Here is an example:
using System.IO;
using System.Xml;
public class Exercise
{
public static int Main(string[] args)
{
string strVideosFile = "Videos.xml";
XmlDocument xdVideos = new XmlDocument();
FileInfo fiVideos = new FileInfo(strVideosFile);
if (fiVideos.Exists == true )
{
XmlTextReader rdrVideos = new XmlTextReader(fiVideos.Name);
do
{
switch (rdrVideos.NodeType)
{
case System.Xml.XmlNodeType.Element:
break;
}
} while (rdrVideos.Read());
}
return 30;
}
}
The Name of an Element
The name of an element is the string that represents the tag. For example, in <Director>, the word Director is the name of the element. An element must have at least a start-tag. All the tags we have seen so far were created as elements. When creating your elements, remember to follow the rules we defined for names.
To support the names of elements, the XmlElement class is equipped with a property named Name. Here is an example of accessing it:
using System; using System.IO; using System.Xml; public class Exercise { public static int Main(string[] args) { string strVideosFile = @"C:\Videos Collection\Videos.xml"; XmlDocument xdVideos = new XmlDocument(); FileInfo fiVideos = new FileInfo(strVideosFile); if (fiVideos.Exists == true) { xdVideos.Load(fiVideos.FullName); XmlElement xeElement = xdVideos.DocumentElement; Console.WriteLine("Element Name: {0}", xeElement.Name); } else Console.WriteLine("The file could not be located."); Console.WriteLine("================================"); return 10; } }
This would produce:
Element Name: videos ================================ Press any key to continue . . .
Notice that videos is returned as the name of the root element of the file. If calling the Read() method of an XmlTextReader object to scan a file, when you get to an element, you can find out its Name identity by accessing it. Consider the following document named Videos.xml:
<?xml version="1.0" encoding="utf-8"?> <Videos> <Video> <Title /> <Director></Director> <LengthInMinutes /> <Format /> <Rating></Rating> <Price /> </Video> <Video> <Title></Title> <Director /> <LengthInMinutes /> <Format></Format> <Rating /> <Price></Price> </Video> </Videos>
Here is an example of getting the names of elements:
using System; using System.IO; using System.Xml; public class Exercise { public static int Main(string[] args) { string strVideosFile = @"C:\Videos Collection\Videos.xml"; FileInfo fiVideos = new FileInfo(strVideosFile); Console.Title = "Videos Collection"; Console.WriteLine("Videos Collection"); Console.WriteLine("------------------------------------------"); if (fiVideos.Exists == true) { XmlTextReader rdrVideos = new XmlTextReader(strVideosFile); do { switch (rdrVideos.NodeType) { case XmlNodeType.Element: Console.WriteLine("Element Name: {0}", rdrVideos.Name); break; } } while (rdrVideos.Read()); } else Console.WriteLine("The file could not be located."); Console.WriteLine("==========================================="); return 10; } }
This would produce:
Videos Collection ------------------------------------------ Element Name: Videos Element Name: Video Element Name: Title Element Name: Director Element Name: LengthInMinutes Element Name: Format Element Name: Rating Element Name: Price Element Name: Video Element Name: Title Element Name: Director Element Name: LengthInMinutes Element Name: Format Element Name: Rating Element Name: Price =========================================== Press any key to continue . . .
The Text or Value of an Element
The value of an element is the item displayed on the right side of the start-tag. It is also called the text of the element. In the case of <director>Jonathan Lynn</director>, the "Jonathan Lynn" string is the value or text of the director element. To support the text or value of an element, the XmlNode class is equipped with a property named Value. The XmlElement inherits this property.
While the value of one element can be a number, the value of another element can be a date. Yet another element can use a regular string as its value. Consider the following example:
<?xml version="1.0" encoding="utf-8"?> <Videos> <Video> <Title>The Distinguished Gentleman</Title> <Director>Jonathan Lynn</Director> <LengthInMinutes>112</LengthInMinutes> <Format>DVD</Format> <Rating>R</Rating> <Price>14.95</Price> </Video> <Video> <Title>Her Alibi</Title> <Director>Bruce Beresford</Director> <LengthInMinutes>94</LengthInMinutes> <Format>VHS</Format> <Rating>PG-13</Rating> <Price>9.95</Price> </Video> </Videos>
Notice that the price elements contain numbers that look like currency values and the LengthInMinutes elements use an integer as value.
If you are using an XmlTextReader object to read a document, when the Read() method gets to an element, you can find out what its value is by accessing this property. Here is an example:
using System; using System.IO; using System.Xml; public class Exercise { public static int Main(string[] args) { string strVideosFile = @"C:\Videos Collection\Videos.xml"; FileInfo fiVideos = new FileInfo(strVideosFile); Console.Title = "Videos Collection"; Console.WriteLine("Videos Collection"); Console.WriteLine("------------------------------------------"); if (fiVideos.Exists == true) { XmlTextReader rdrVideos = new XmlTextReader(strVideosFile); do { switch (rdrVideos.NodeType) { case XmlNodeType.Text: Console.WriteLine("Element Value: {0}", rdrVideos.Value); break; } } while (rdrVideos.Read()); } else Console.WriteLine("The file could not be located."); Console.WriteLine("==========================================="); return 10; } }
This would produce:
Videos Collection ------------------------------------------ Element Value: The Distinguished Gentleman Element Value: Jonathan Lynn Element Value: 112 Element Value: DVD Element Value: R Element Value: 14.95 Element Value: Her Alibi Element Value: Bruce Beresford Element Value: 94 Element Value: VHS Element Value: PG-13 Element Value: 9.95 =========================================== Press any key to continue . . .
The value or text of an element is based on a class named XmlText.
Empty Elements
An element may not have a value but only a name. Consider the following example:
<?xml version="1.0" encoding="utf-8"?> <videos> <video> <title>The Distinguished Gentleman</title> <director>Jonathan Lynn</director> </video> </videos>
In this case, the video element doesn't have a value. It is called an empty element. When a tag is empty, the Value property of its XmlElement object would return an empty value.
Character Entities in an Element Value
Besides the obvious types of values, you may want to display special characters as values of elements. Consider the following example:
<?xml version="1.0" encoding="utf-8" ?>
<Employees>
<Employee>
<FullName>Sylvie <Bertine> Aronson</FullName>
<Salary>25.64</Salary>
<DepartmentID>1</DepartmentID>
</Employee>
<Employee>
<FullName>Bertrand Yamaguchi</FullName>
<Salary>16.38</Salary>
<DepartmentID>4</DepartmentID>
</Employee>
</Employees>
If you try using this XML document, for example, if you try displaying it in a browser, you would receive an error.
To display a special symbol as part of a value, you can use its character code. For example, the < (Less Than) character is represented with < and the > (Greater Than) symbol can be used with >. Therefore, the above code can be corrected as follows:
<?xml version="1.0" encoding="utf-8" ?> <Employees> <Employee> <FullName>Sylvie <Bertine> Aronson</FullName> <Salary>25.64</Salary> <DepartmentID>1</DepartmentID> </Employee> <Employee> <FullName>Bertrand Yamaguchi</FullName> <Salary>16.38</Salary> <DepartmentID>4</DepartmentID> </Employee> </Employees>
Here is a list of other codes you can use for special characters:
Code | Symbol | Code | Symbol | Code | Symbol | Code | Symbol | Code | Symbol |
' | ' | C | C | j | j | ³ | ³ | Ú | Ú |
< | < | D | D | k | k | ´ | ´ | Û | Û |
> | > | E | E | l | l | µ | µ | Ü | Ü |
& | & | F | F | m | m | ¶ | ¶ | Ý | Ý |
" | " | G | G | n | n | · | · | Þ | Þ |
! | ! | H | H | o | o | ¸ | ¸ | ß | ß |
" | " | I | I | p | p | ¹ | ¹ | à | à |
# | # | J | J | q | q | º | º | á | á |
$ | $ | K | K | r | r | » | » | â | â |
% | % | L | L | s | s | ¼ | ¼ | ã | ã |
& | & | M | M | t | t | ½ | ½ | ä | ä |
' | ' | N | N | u | u | ¾ | ¾ | å | å |
( | ( | O | O | v | v | ¿ | ¿ | æ | æ |
) | ) | P | P | w | w | À | À | ç | ç |
* | * | Q | Q | x | x | Á | Á | è | è |
+ | + | R | R | y | y | Â | Â | é | é |
, | , | S | S | z | z | Ã | Ã | ê | ê |
- | - | T | T | { | { | Ä | Ä | ë | ë |
. | . | U | U | } | } | Å | Å | ì | ì |
/ | / | V | V | ~ | ~ | Æ | Æ | í | í |
0 | 0 | W | W |   | empty | Ç | Ç | î | î |
1 | 1 | X | X | ¡ | ¡ | È | È | ï | ï |
2 | 2 | Y | Y | ¢ | ¢ | É | É | ð | ð |
3 | 3 | Z | Z | £ | £ | Ê | Ê | ñ | ñ |
4 | 4 | [ | [ | ¤ | ¤ | Ë | Ë | ò | ò |
5 | 5 | \ | \ | ¥ | ¥ | Ì | Ì | ó | ó |
6 | 6 | ] | ] | ¦ | ¦ | Í | Í | ô | ô |
7 | 7 | ^ | ^ | § | § | Î | Î | õ | õ |
8 | 8 | _ | _ | ¨ | ¨ | Ï | Ï | ö | ö |
9 | 9 | ` | ` | © | © | Ð | Ð | ÷ | ÷ |
: | : | a | a | ª | ª | Ñ | Ñ | ø | ø |
; | ; | b | b | « | « | Ò | Ò | ù | ù |
< | < | c | c | ¬ | ¬ | Ó | Ó | ú | ú |
= | = | d | d | ­ | | Ô | Ô | û | û |
> | > | e | e | ® | ® | Õ | Õ | ü | ü |
? | ? | f | f | ¯ | ¯ | Ö | Ö | ý | ý |
@ | @ | g | g | ° | ° | × | × | þ | þ |
A | A | h | h | ± | ± | Ø | Ø | ÿ | ÿ |
B | B | i | i | ² | ² | Ù | Ù | Ā | Ā |
There are still other codes to include special characters in an XML file.
Identifying the Markup of a Node
The Inner Text of a Node
To let you get the combined text of the values of the children of a node, the XmlNode class provides a property named InnerText. It is declared as follows:
public virtual string InnerText { get; set; }
This property concatenates the values of the children of the node that called them but doesn't include their markups. Here is an example:
using System; using System.IO; using System.Xml; using System.Windows.Forms; namespace VideosCollection { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnVideosCollection_Click(object sender, EventArgs e) { XmlDocument xdVideos = new XmlDocument(); string strVideosFile = @"C:\Videos Collection\Videos.xml"; FileInfo fiVideos = new FileInfo(strVideosFile); if (fiVideos.Exists == true) { xdVideos.Load(strVideosFile); XmlElement xeVideo = xdVideos.DocumentElement; MessageBox.Show(string.Format("Inner Text: {0}", xeVideo.InnerText), "Videos Collection"); } else MessageBox.Show("The file could not be located.", "Videos Collection"); } } }
This would produce:
Notice that this property produces all values of the children of a node in one block. We already saw how to access each value of the children of a node by calling the XmlTextReader.Read() method and get its Text value.
The Outer XML Code of a Node
To let you get a node, its markup, its child(ren) and its(their) markup(s), the XmlNode class provides a property named OuterXml. It is declared as follows:
public virtual string OuterXml { get; }
Here is an example:
using System; using System.IO; using System.Xml; using System.Windows.Forms; namespace VideosCollection { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnVideosCollection_Click(object sender, EventArgs e) { XmlDocument xdVideos = new XmlDocument(); string strVideosFile = @"C:\Videos Collection\Videos.xml"; FileInfo fiVideos = new FileInfo(strVideosFile); if (fiVideos.Exists == true) { xdVideos.Load(strVideosFile); XmlElement xeVideo = xdVideos.DocumentElement; MessageBox.Show(string.Format("Outer XML: {0}", xeVideo.OuterXml), "Videos Collection"); } else MessageBox.Show("The file could not be located.", "Videos Collection"); } } }
This would produce:
The Inner XML Code of a Node
To let you get only the markup(s) of the child(ren) excluding the parent, the XmlNode class provides a property named InnerXml. It is declared as follows:
public virtual string InnerXml { get; set; }
Here is an example:
using System; using System.IO; using System.Xml; using System.Windows.Forms; namespace VideosCollection { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnVideosCollection_Click(object sender, EventArgs e) { XmlDocument xdVideos = new XmlDocument(); string strVideosFile = @"C:\Videos Collection\Videos.xml"; FileInfo fiVideos = new FileInfo(strVideosFile); if (fiVideos.Exists == true) { xdVideos.Load(strVideosFile); XmlElement xeVideo = xdVideos.DocumentElement; MessageBox.Show(string.Format("Inner XML: {0}", xeVideo.InnerXml), "Videos Collection"); } else MessageBox.Show("The file could not be located.", "Videos Collection"); } } }
This would produce:
Practical Learning: Ending the Lesson
|
||
Previous | Copyright © 2005-2021, FunctionX | Next |
|