Accessing an Element s Attributes


Accessing an Element's Attributes

In most cases, navigating an XmlDocument node tree involves moving through parent-child relationships, except when you are accessing an element's attributes. The element-attribute relationship is a container-contained relationship. Since the XmlNode is an abstract class that represents the common functionality of all XML nodes, it provides only minimal functionality for accessing attribute information. Since attributes are most meaningful on XML elements, the XmlElement class provides a richer API for retrieving attribute information. In this section we will investigate the methods available on both classes.

The XmlNode class exposes the Attributes property to access its attributes. The Attributes property returns an AttributeCollection object that represents all of the XmlAttributes of an XmlElement . This includes namespace declarations, as well. You can iterate through the attribute nodes in two ways. You can iterate using the foreach statement or a for loop in combination with the Count and Item properties. Listing 11.12 illustrates each technique.

Listing 11.12
 C# XmlDocument doc = new XmlDocument(); doc.LoadXml("<root att1=\"val1\" att2=\"val2\" />"); XmlNode node = doc.DocumentElement; foreach(XmlAttribute att in node.Attributes) {   MessageBox.Show(att.Name + ":" + att.Value); } for(int ndx = 0; ndx < node.Attributes.Count; ++ndx) {   XmlAttribute att = node.Attributes[ndx];   MessageBox.Show(att.Name + ":" + att.Value); } VB Dim doc As New XmlDocument doc.LoadXml("<root att1=" & Chr(34) & "val1" & Chr(34) & _             "att2=" & Chr(34) & "val2" & Chr(34) & "/>") Dim node As XmlElement node = doc.DocumentElement For Each xmlAtt As XmlAttribute In node.Attributes   MessageBox.Show(xmlAtt.Name & ":" & xmlAtt.Value) Next Dim i As Int32 For i = 0 To node.Attributes.Count   Dim att As XmlAttribute   att = node.Attributes(i)   MessageBox.Show(att.Name & ":" & att.Value) Next i 

You can access the value of the attribute by using the Value property, and you can access the name of the attribute by using the Name property. The Attributes property can also be used to access specific attributes by name. This is illustrated in Listing 11.13.

Listing 11.13
 C# XmlDocument doc = new XmlDocument(); doc.LoadXml("<root att1=\"val1\" att2=\"val2\" />"); XmlNode node = doc.DocumentElement; XmlAttribute att = node.Attributes["att1"]; MessageBox.Show(att.Name + ":" + att.Value); VB Dim doc As New XmlDocument(); doc.LoadXml("<root att1="&Chr(34)&"val1"&Chr(34)& _             "att2="&Chr(34)&"val2"&Chr(34)& "/>") Dim node AS doc.DocumentElement Dim att As node.Attributes("att1") MessageBox.Show(att.Name & ":" & att.Value) 

The XmlElement class provides a richer API for accessing XmlAttributes . In addition to the Attributes property, the XmlElement class provides the HasAttributes property to find out whether an XmlElement contains any XmlAttributes . There is also the HasAttribute property, which determines whether the XmlElement has an XmlAttribute with a particular name.

Listing 11.14
 C# XmlDocument doc = New XmlDocument(); doc.LoadXml("<root att1=\"val1\" att2=\"val2\" />"); XmlElement elem = doc.DocumentElement; MessageBox.Show("Contains Attributes: " + elem.HasAttributes); MessageBox.Show("Contains att1: " + elem.HasAttribute("att1")); VB Dim doc As New XmlDocument() doc.LoadXml("<root att1="&Chr(34)&"val1"&Chr(34)& _             "att2="&Chr(34)&"val2"&Chr(34)& "/>") Dim elem As doc.DocumentElement MessageBox.Show("Contains Attributes: " & elem.HasAttributes) MessageBox.Show("Contains att1: " & elem.HasAttribute("att1")) 

The XmlElement provides the GetAttribute method, which returns the value of a specified attribute, and the GetAttributeNode method, which returns an XmlAttribute object representing the specified attributes. The GetAttribute method is very useful if you want to retrieve only the value of the attribute. This method will locate the attribute and pull out the value of the attribute. The GetAttributeNode method is also helpful if you want to get the XmlAttribute object without having to interact with an XmlAttributeCollection object. These methods are used in Listing 11.15.

Listing 11.15
 C# XmlDocument doc = new XmlDocument(); doc.LoadXml("<root att1=\"val1\" att2=\"val2\" />"); XmlElement elem = doc.DocumentElement; MessageBox.Show("Attribute Value: " + elem.GetAttribute("att1")); XmlAttribute att = elem.GetAttributeNode("att2"); MessageBox.Show("Attribute Value: " + att.Value); VB Dim doc As New XmlDocument() doc.LoadXml("<root att1="&Chr(34)&"val1"&Chr(34)& _             "att2="&Chr(34)&"val2"&Chr(34)& "/>") Dim elem As doc.DocumentElement MessageBox.Show("Attribute Value: " & elem.GetAttribute("att1")) Dim att As elem.GetAttributeNode("att2") MessageBox.Show("Attribute Value: " & att.Value) 


Microsoft.NET Compact Framework Kick Start
Microsoft .NET Compact Framework Kick Start
ISBN: 0672325705
EAN: 2147483647
Year: 2003
Pages: 206

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net