Share via


Object Comparison Using XmlNameTable

XmlDocuments, when created, have a name table created specifically for that document. When XML is loaded into the document, or new elements or attributes are created, the attribute and element names are put into the XmlNameTable. You can also create an XmlDocument using an existing NameTable from another document. When XmlDocuments are created with the constructor that takes an XmlNameTable parameter, the document has access to the node names, namespaces, and prefixes already stored in the XmlNameTable. Regardless of how the name table is loaded with names, once the names are stored in the table, names can be compared quickly using object comparison instead of string comparison. Strings can also be added to the name table using the NameTable.Add Method. The following code sample shows a name table being created and the string MyString being added to the table. After that, an XmlDocument is created using that table, and the element and attribute names in Myfile.xml are added to the existing name table.

Dim nt As New NameTable()
nt.Add("MyString")
Dim doc As New XmlDocument(nt)
doc.Load("Myfile.xml")
NameTable nt = new NameTable();
nt.Add("MyString");
XmlDocument doc = new XmlDocument(nt);
doc.Load("Myfile.xml");

The following code example shows the creation of a document, two new elements being added to the document, which also adds them to the document name table, and the object comparison on the names.

Dim doc1 As XmlDocument = imp.CreateDocument()
Dim node1 As XmlElement = doc.CreateElement("node1")
Dim doc2 As XmlDocument = imp.CreateDocument()
Dim node2 As XmlElement = doc.CreateElement("node2")
if (CType(node1.Name, object) = CType(node2.Name, object))
XmlDocument doc1 = imp.CreateDocument();
node1 = doc1.CreateElement ("node1");
XmlDocument doc2 = imp.CreateDocument();
node2 = doc2.CreateElement ("node1");
if (((object)node1.Name) == ((object)node2.Name))
{ ...

The above scenario of a name table passed between two documents is typical when the same type of document is being processed repeatedly, such as order documents at an ecommerce site, which conform to an XML Schema definition language (XSD) schema or document type definition (DTD) and the same strings are repeated. Using the same name table gives a performance improvement, as the same element name occurs in multiple documents.

See Also

Concepts

XML Document Object Model (DOM)