System.Xml.NameTable Class

public class NameTable : XmlNameTable

Base Types

Object
  XmlNameTable
    NameTable

Assembly

System.Xml

Library

XML

Summary

Creates a table that stores unique instances of String objects.

Description

Only a single instance of any given string is stored even if the string is added multiple times to the table.

Using this class provides an efficient means for an XML parser to use the same String object for all repeated element and attribute names in an XML document. If the same object is used for all repeated names, the efficiency of name comparisons is increased by allowing the names to be compared using object comparisons rather than string comparisons.

[Note: This class implements a single-threaded XmlNameTable .

This class is used internally by the XmlNamespaceManager, XmlParserContext, and XmlTextReader classes to store element and attribute names.

]

Example

The following example demonstrates the difference between equal string values and equal String objects using the NameTable class.

using System;
using System.Text;
using System.Xml;

class Ntable {

  public static void Main() {

    NameTable nameTable = new NameTable();

    string str1 = "sunny";
    StringBuilder strBuilder = new StringBuilder();
    string str2 = 
      strBuilder.Append("sun").Append("ny").ToString();
    Console.WriteLine( "{0} : {1}",
                       str1, str2 );
    Console.WriteLine( "{0} : {1}",
                       str1 == str2,
                       (Object)str1==(Object)str2 );

    string str3 = nameTable.Add(str1);
    string str4 = nameTable.Add(str2);
    Console.WriteLine( "{0} : {1}",
                       str3, str4 );
    Console.WriteLine( "{0} : {1}",
                       str3 == str4,
                       (Object)str3==(Object)str4 );
  }
}
   
The output is

sunny : sunny

True : False

sunny : sunny

True : True

See Also

System.Xml Namespace

Members

NameTable Constructors

NameTable Constructor

NameTable Methods

NameTable.Add(System.String) Method
NameTable.Add(char[], int, int) Method
NameTable.Get(System.String) Method
NameTable.Get(char[], int, int) Method


NameTable Constructor

public NameTable();

Summary

Constructs and initializes a new instance of the NameTable class.

See Also

System.Xml.NameTable Class, System.Xml Namespace

NameTable.Add(System.String) Method

public override string Add(string key);

Summary

Adds the specified String to the table if a String instance with the same value does not already exist in the table.

Parameters

key
The String to add.

Return Value

key, if it did not exist in the table at the time of the call, or the String instance previously stored in the table with a value equal to key.

Exceptions

Exception TypeCondition
ArgumentNullExceptionkey is null .

Description

Only a single instance of any given String is stored in the table. If the value of key is already stored in the table, the String instance with that value is returned.

[Note: This method overrides System.Xml.XmlNameTable.Add(System.Char[],System.Int32,System.Int32)(String ).

]

See Also

System.Xml.NameTable Class, System.Xml Namespace

NameTable.Add(char[], int, int) Method

public override string Add(char[] key, int start, int len);

Summary

Adds the String equivalent of a specified subset of a Char array to the table if the string equivalent does not already exist in the table.

Parameters

key
A Char array containing the string to add.
start
A Int32 specifying the zero-based index into the array of the first character of the string.
len
A Int32 containing the number of characters in the string.

Return Value

The String equivalent of the specified subset of the Char array that is stored in the table, or System.String.Empty if len is zero.

Exceptions

Exception TypeCondition
IndexOutOfRangeExceptionstart < 0.

- or -

start >= key.Length.

- or -

len > key.Length - start.

The above conditions do not cause an exception to be thrown if len = 0.

ArgumentOutOfRangeExceptionlen < 0.

Description

Only a single instance of any given String is stored in the table. Calling this method with the same subset (containing the same characters) of any Char array, returns the same instance of the String equivalent.

[Note: This method overrides System.Xml.XmlNameTable.Add(System.Char[],System.Int32,System.Int32)(Char [], Int32 , Int32 ).

]

See Also

System.Xml.NameTable Class, System.Xml Namespace

NameTable.Get(System.String) Method

public override string Get(string value);

Summary

Looks up the value of the specified String in the table.

Parameters

value
The String to look up.

Return Value

The String instance previously stored in the table with a value equal to value , or null if it does not exist.

Exceptions

Exception TypeCondition
ArgumentNullExceptionvalue is null .

Description

Only a single instance of any given String is stored in the table. If the value of value is already stored in the table, the String instance with that value is returned.

[Note: This method overrides System.Xml.XmlNameTable.Get(System.Char[],System.Int32,System.Int32)(String ).

]

See Also

System.Xml.NameTable Class, System.Xml Namespace

NameTable.Get(char[], int, int) Method

public override string Get(char[] key, int start, int len);

Summary

Looks up the String equivalent to a specified subset of a Char array in the table.

Parameters

key
A Char array containing the string to look up.
start
A Int32 specifying the zero-based index into the array of the first character of the string.
len
A Int32 containing the number of characters in the string.

Return Value

The String equivalent of the specified subset of the Char array that is stored in the table, or null if the equivalent String is not in the table.

Exceptions

Exception TypeCondition
IndexOutOfRangeExceptionstart

< 0.

- or -

start >= key.Length.

- or -

len > key.Length - start.

The above conditions do not cause an exception to be thrown if len = 0.

ArgumentOutOfRangeExceptionlen < 0.

Description

Only a single instance of any given String is stored in the table. Calling this method with the same subset (containing the same characters) of any Char array, returns the same instance of the String equivalent, if it exists.

[Note: This method overrides System.Xml.XmlNameTable.Get(System.Char[],System.Int32,System.Int32)(Char [], Int32 , Int32 ).

]

See Also

System.Xml.NameTable Class, System.Xml Namespace