Introduction to XML Elements |
|
Elements Fundamentals |
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 the XmlElement class. XmlElement is based on a class named XmlLinkedNode that itself is based on XmlNode. 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. In the previous lesson, we saw that every XML file must have a root and we mentioned that you could call the XmlDocument.DocumentElement property to access it. This property is of type XmlElement and, to access it, you can declare an XmlElement variable and assign it this property. Here is an example:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; using System.Xml; namespace VideoCollection1 { public partial class Exercise : Form { public Exercise() { InitializeComponent(); } private void btnDocument_Click(object sender, EventArgs e) { XmlDocument docVideo = new XmlDocument(); string strFilename = "videos.xml"; if (File.Exists(strFilename)) { docVideo.Load(strFilename); XmlElement elm = docVideo.DocumentElement; } else MessageBox.Show("The file " + strFilename + " was not found"); } } } An XML element is represented in the XmlNodeType enumeration as the Element member. When using the Read() method of an XmlTextReader object, 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: private void btnDocument_Click(object sender, EventArgs e) { XmlDocument docVideo = new XmlDocument(); string strFilename = "videos.xml"; if (File.Exists(strFilename)) { XmlTextReader rdrVideos = new XmlTextReader(strFilename); do { switch (rdrVideos.NodeType) { case XmlNodeType.Element: break; } } while (rdrVideos.Read()); } else MessageBox.Show("The file " + strFilename + " was not found"); }
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 of the tags we have seen so far were created as elements. When creating your elements, remember to follow the rules we defined for names. The XmlElement class is equipped with the Name property that can be used to identify an existing element. Here is an example of accessing it: private void btnDocument_Click(object sender, EventArgs e) { XmlDocument docVideo = new XmlDocument(); string strFilename = "videos.xml"; if (File.Exists(strFilename)) { docVideo.Load(strFilename); XmlElement elm = docVideo.DocumentElement; MessageBox.Show(elm.Name); } else MessageBox.Show("The file " + strFilename + " was not found"); } This would produce: 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. Here is an example: private void btnDocument_Click(object sender, EventArgs e) { XmlDocument docVideo = new XmlDocument(); string strFilename = "videos.xml"; if (File.Exists(strFilename)) { XmlTextReader rdrVideos = new XmlTextReader(strFilename); do { switch (rdrVideos.NodeType) { case XmlNodeType.Element: MessageBox.Show(rdrVideos.Name); break; } } while (rdrVideos.Read()); } else MessageBox.Show("The file " + strFilename + " was not found"); }
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 of the director element. To support the text or value of an element, the XmlElement class is equipped with the Value 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 scan a file, when the Read() method gets to an element, you can find out what its value is by accessing this property. Here is an example: private void btnDocument_Click(object sender, EventArgs e) { XmlDocument docVideo = new XmlDocument(); string strFilename = "videos.xml"; if (File.Exists(strFilename)) { XmlTextReader rdrVideos = new XmlTextReader(strFilename); do { switch (rdrVideos.NodeType) { case XmlNodeType.Text: MessageBox.Show(rdrVideos.Value); break; } } while (rdrVideos.Read()); } else MessageBox.Show("The file " + strFilename + " was not found"); } The value or text of an element is an object of type XmlText.
An element may not have a value but only a name. Consider the following example: <?xml version="1.0" encoding="utf-8"?> <videos> <video> <vitle>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.
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 <Bellie> 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:
The reason is that when the parser reaches the <FullName>Sylvie <Bellie> Aronson</FullName> line, it thinks that <Bellie> is a tag but then <Bellie> is not closed. The parser concludes that the document is not well-formed, that there is an error. For this reason, 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 <Bellie> Aronson</FullName> <Salary>25.64</Salary> <DepartmentID>1</DepartmentID> </Employee> <Employee> <FullName>Bertrand Yamaguchi</FullName> <Salary>16.38</Salary> <DepartmentID>4</DepartmentID> </Employee> </Employees> This would produce:
Here is a list of other codes you can use for special characters:
There are still other codes to include special characters in an XML file.
In the previous sections, we have seen how to create a tag to produce a node. We also saw that a node could be placed inside of another node. The combined text of the values of the children of a node is available through its XmlNode.InnerText property which 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: private void btnDocument_Click(object sender, EventArgs e) { XmlDocument docVideo = new XmlDocument(); string strFilename = "videos.xml"; if (File.Exists(strFilename)) { docVideo.Load(strFilename); XmlElement elm = docVideo.DocumentElement; rchDocument.Text = elm.InnerText; } else MessageBox.Show("The file " + strFilename + " was not found"); } 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.
If you want to get a node, its markup, its child(ren) and its(their) markup(s), you can access its XmlNode.OuterXml property which is declared as follows: public virtual string OuterXml{get}; Here is an example: private void btnDocument_Click(object sender, EventArgs e) { XmlDocument docVideo = new XmlDocument(); string strFilename = "videos.xml"; if (File.Exists(strFilename)) { docVideo.Load(strFilename); XmlElement elm = docVideo.DocumentElement; rchDocument.Text = elm.OuterXml; } else MessageBox.Show("The file " + strFilename + " was not found"); } This would produce:
If you want only the markup(s) of the child(ren) excluding the parent, access its XmlNode.InnerXml property which is declared as follows: public virtual string InnerXml{get}; Here is an example: private void btnDocument_Click(object sender, EventArgs e) { XmlDocument docVideo = new XmlDocument(); string strFilename = "videos.xml"; if (File.Exists(strFilename)) { docVideo.Load(strFilename); XmlElement elm = docVideo.DocumentElement; rchDocument.Text = elm.InnerXml; } else MessageBox.Show("The file " + strFilename + " was not found"); } This would produce: |
|
||
Previous | Copyright © 2007-2013, FunctionX | Next |
|