
Retrieving All Attributes into a Collection
If you want all the attributes of an element node put into a collection, call the XmlElement.Attributes property. This gets the XmlAttributeCollection that contains all the attributes of an element. The XmlAttributeCollection class inherits from the XmlNamedNode map. Therefore, the methods and properties available on the collection include those available on a named node map in addition to methods and properties specific to the XmlAttributeCollection class, such as the ItemOf property or the Append method. Each item in the attribute collection represents an XmlAttribute node. To find the number of attributes on an element, get the XmlAttributeCollection, and use the Count property to see how many XmlAttribute nodes are in the collection.
The following code example shows how to retrieve an attribute collection and, using the Count method for the looping index, iterate over it. The code then shows how to retrieve a single attribute from the collection and display its value.
Imports System
Imports System.IO
Imports System.Xml
Public Class Sample
Public Shared Sub Main()
Dim doc As XmlDocument = New XmlDocument()
doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5' misc='sale item'>" & _
"<title>The Handmaid's Tale</title>" & _
"<price>14.95</price>" & _
"</book>")
' Move to an element.
Dim myElement As XmlElement = doc.DocumentElement
' Create an attribute collection from the element.
Dim attrColl As XmlAttributeCollection = myElement.Attributes
' Show the collection by iterating over it.
Console.WriteLine("Display all the attributes in the collection...")
Dim i As Integer
For i = 0 To attrColl.Count - 1
Console.Write("{0} = ", attrColl.ItemOf(i).Name)
Console.Write("{0}", attrColl.ItemOf(i).Value)
Console.WriteLine()
Next
' Retrieve a single attribute from the collection; specifically, the
' attribute with the name "misc".
Dim attr As XmlAttribute = attrColl("misc")
' Retrieve the value from that attribute.
Dim miscValue As String = attr.InnerXml
Console.WriteLine("Display the attribute information.")
Console.WriteLine(miscValue)
End Sub
End Class
using System;
using System.IO;
using System.Xml;
public class Sample
{
public static void Main()
{
XmlDocument doc = new XmlDocument();
doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5' misc='sale item'>" +
"<title>The Handmaid's Tale</title>" +
"<price>14.95</price>" +
"</book>");
// Move to an element.
XmlElement myElement = doc.DocumentElement;
// Create an attribute collection from the element.
XmlAttributeCollection attrColl = myElement.Attributes;
// Show the collection by iterating over it.
Console.WriteLine("Display all the attributes in the collection...");
for (int i = 0; i < attrColl.Count; i++)
{
Console.Write("{0} = ", attrColl[i].Name);
Console.Write("{0}", attrColl[i].Value);
Console.WriteLine();
}
// Retrieve a single attribute from the collection; specifically, the
// attribute with the name "misc".
XmlAttribute attr = attrColl["misc"];
// Retrieve the value from that attribute.
String miscValue = attr.InnerXml;
Console.WriteLine("Display the attribute information.");
Console.WriteLine(miscValue);
}
}
This example displays the following output:
Output
Display all the attributes in the collection.
genre = novel
ISBN = 1-861001-57-5
misc = sale item
Display the attribute information.
sale item
The information in an attribute collection can be retrieved by name or index number. The example above shows how to retrieve data by name. The next example shows how to retrieve data by index number.
Because the XmlAttributeCollection is a collection and can be iterated over by name or index, this example shows selecting the first attribute out of the collection using a zero-based index and using the following file, baseuri.xml, as input.
Input
<!-- XML fragment -->
<book genre="novel">
<title>Pride And Prejudice</title>
</book>
Option Explicit On
Option Strict On
Imports System
Imports System.IO
Imports System.Xml
Public Class Sample
Public Shared Sub Main()
' Create the XmlDocument.
Dim doc As New XmlDocument()
doc.Load("http://localhost/baseuri.xml")
' Display information on the attribute node. The value
' returned for BaseURI is 'http://localhost/baseuri.xml'.
Dim attr As XmlAttribute = doc.DocumentElement.Attributes(0)
Console.WriteLine("Name of the attribute: {0}", attr.Name)
Console.WriteLine("Base URI of the attribute: {0}", attr.BaseURI)
Console.WriteLine("The value of the attribtue: {0}", attr.InnerText)
End Sub 'Main
End Class 'Sample
using System;
using System.IO;
using System.Xml;
public class Sample
{
public static void Main()
{
// Create the XmlDocument.
XmlDocument doc = new XmlDocument();
doc.Load("http://localhost/baseuri.xml");
// Display information on the attribute node. The value
// returned for BaseURI is 'http://localhost/baseuri.xml'.
XmlAttribute attr = doc.DocumentElement.Attributes[0];
Console.WriteLine("Name of the attribute: {0}", attr.Name);
Console.WriteLine("Base URI of the attribute: {0}", attr.BaseURI);
Console.WriteLine("The value of the attribtue: {0}", attr.InnerText);
}
}