Object Comparison Using XmlNameTable with XmlReader

The XmlNameTable class enables the implementations of the XmlReader class to use pointer comparisons instead of string comparisons when parsing data or performing comparison operations on XML documents. The use of this table gives performance gains to the XmlReader derived classes when comparing and using element and attribute names. The XmlNameTable also reduces the amount of memory allocated during parsing because each element or attribute name is allocated only once. The same instance of the string object that stores the name is used if the same name occurs multiple times in the XML document.

Note

In the .NET Framework version 2.0, the recommended practice is to create XmlReader instances using the XmlReaderSettings class and the Create method. This allows you to take full advantage of all the new features introduced in the .NET Framework. For more information, see Creating XML Readers.

Using the XmlNameTable

The XmlNameTable is an abstract base class with NameTable as an implementation. The NameTable contains atomized versions of element and attribute names, along with namespace URIs and prefixes. If the application is doing a lot of comparisons on element or attribute names, the application should use the NameTable from the XmlReader. The user can get the NameTable that the reader is using from the XmlReader.NameTable property. For a description of atomization, see XmlNameTable.

The application can add names to the table with the Add method. The following example shows that comparisons are then done by using the Equals method or == operator, which determines whether the object given in the method call is the same instance as the current object.

Dim cust As Object = reader.NameTable.Add("Customer")
While reader.Read()
   ' The "if" uses efficient pointer comparison.
   If cust Is reader.Name Then
      ...
   End If
End While
object cust = reader.NameTable.Add("Customer");
while (reader.Read())
{
   // The "if" uses efficient pointer comparison.
   if (cust == reader.Name)   
   {
      ...
   }
}

See Also

Concepts

Reading XML with the XmlReader

Other Resources

Using the XmlReader Class