Introduction to XML Elements |
|
An element in an XML document is an object that starts with a start-tag, may contain a value, and may end 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.
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.
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" ?> <Employees> <Employee> <FullName>Lydia Thomason</FullName> <Salary>25.64</Salary> <DepartmentID>1</DepartmentID> </Employee> <Employee> <FullName>June Grath</FullName> <Salary>16.38</Salary> <DepartmentID>4</DepartmentID> </Employee> </Employees> Notice that the Salary elements contain numbers that look like currency values and the DepartmentID elements use an integer as value. The value or text of an element is an object of type XmlText.
Besides these 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. There is a list of parent of XML, provides a list of the characters for all special symbols. For example, the left angle bracket can be represented with < while the right angle bracket can be represented with >. Based on this, 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: XML also provides its own code to include special characters in your XML files.
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 but it is an element in its own right.
An XML file appears as an upside-down tree: it has a root, can have branches and leaves. As we have seen in the previous lesson, all of these objects are created using the same technique: a tag with a name and an optional value. Based on their similarities, these objects can be referred to as nodes. Here is an example: <?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> </Videos> To support nodes of an XML file, the .NET Framework provides the XmlNode class, which is the ancestor to all types of nodes. As seen in the previous lesson, one node can be nested inside of another. In that case, the nested node is called a child of the nesting node, which is a parent node. This also implies that a node can have as many children as necessary, making them child nodes of the parent node. In the above example, the Title and the Director nodes are children of the Video node. The Video node is the parent of both the Title and the Director node.
To support the child nodes of a particular node, the XmlNode class is equipped with the ChildNodes property. To get the whole text of the values of the children of a node, access its InnerText property which is declared as follows: Public Overridable Property InnerText As String If you want only the markup(s) of the child(ren) excluding the parent, access its InnerXml property which is declared as follows: Public Overridable Property InnerXml As String The children of a nesting node are also recognized by their sequence. Based on this, the first node is called the first child. As opposed to the first child, the child node that immediately precedes the end-tag of the parent node is called the last child. To get the last child of a node, you can access its XmlNode.LastChild property that is declared as follows: Public Overridable ReadOnly Property LastChild As XmlNode The child nodes that are nested in a parent node and share the same level are referred to as siblings. Consider the following listing: <?xml version="1.0" encoding="utf-8"?> <Videos> <Video> <Title>The Distinguished Gentleman</Title> <Director>Jonathan Lynn</Director> <Actors> <Actor>Eddie Murphy</Actor> <Actor>Lane Smith</Actor> <Actor>Sheryl Lee Ralph</Actor> <Actor>Joe Don Baker</Actor> </Actors> <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> </Videos> In this example, Director, Actors, and Length are child nodes of the Video node but the Actor node is not a child of the Video node. Consequently, Director, Actors, and Length are siblings. Obviously, to get a sibling, you must first have a node. Based on this, to access the sibling of a node, you can use its NextSibling property, which is declared as follows: Public Overridable ReadOnly Property NextSibling As XmlNode
|
|
||
Previous | Copyright © 2005-2016, FunctionX | Next |
|