Class Reference
This section provides a reference to the key objects described in this chapter.
Inheritance Relationships
This chapter covers three classes involved in XML handling in the .NET framework. The XmlDocument object provides access to the XML DOM through .NET. The XmlTextReader and DocumentNavigator classes are a .NET-specific way to handle XML documents.
XmlTextReader and DocumentNavigator both inherit from abstract classes found in the System.Xml namespace. Figure 11.3 shows the inheritance relationship between these objects.
Figure 11.3 Inheritance relationships between proprietary .NET XML handler classes
NOTE
Because the DocumentNavigator class inherits from the abstract XmlNavigator class, it implies that there could be other subclassed implementations of XmlNavigator. In fact, the .NET framework contains just such a class, the DataDocumentNavigator, discussed in Chapter 12.
To make this reference more concise, we omit descriptions for inherited properties, methods and events that are not significantly different from those found in base classses. In the following listings, such members are displayed in italic type.
XmlDocument Object
This object is a member of System.Xml.
The XmlDocument object represents the top-level object in the XML DOM hierarchy. Properties and methods listed in italics are not described in this section, typically because they are inherited or otherwise duplicated from base classes described elsewhere.
Constructor Examples
xd = New System.Xml.XmlDocument() xd = New System.Xml.XmlDocument(XmlNameTable)
Properties
Attributes |
IsDocumentReadOnly |
NodeType |
ChildNodes |
IsReadOnly |
OuterXml |
DocumentElement |
Item |
OwnerDocument |
DocumentType |
LastChild |
ParentNode |
FirstChild |
LocalName |
Prefix |
HasChildNodes |
Name |
PreserveWhitespace |
Implementation |
NamespaceURI |
PreviousSibling |
InnerText |
NameTable |
Value |
InnerXml |
NextSibling |
|
Methods
AppendChild |
CreateWhitespace |
Load |
Clone |
CreateXmlDeclaration |
LoadXml |
CloneNode |
Equals |
MemberwiseClone |
CreateAttribute |
Finalize |
Normalize |
CreateCDataSection |
GetElementById |
PrependChild |
CreateComment |
GetElementsByTagName |
ReadNode |
CreateDocumentFragment |
GetEnumerator |
RemoveAll |
CreateDocumentType |
GetHashCode |
RemoveChild |
CreateElement |
GetNamespaceOfPrefix |
ReplaceChild |
CreateEntityReference |
GetPrefixOfNamespace |
Save |
CreateNode |
GetType |
Supports |
CreateProcessingInstruction |
ImportNode |
ToString |
CreateSignificantWhitespace |
InsertAfter |
WriteContentTo |
CreateTextNode |
InsertBefore |
WriteTo |
AppendChild Method
The AppendChild method adds a new node to the end of the XML document. It is inherited from System.Xml.XmlNode. The method takes an XmlNode object as its sole argument and returns an XmlNode object.
Note that in general, unless you're using AppendChild to construct an XML document from scratch, you don't want to use the AppendChild method against the document itself. (This is an illegal operation, because an XML document can only contain a single root node.) To add nodes to an existing document, use the AppendChild method of the document's root node (accessible through its FirstChild property).
An example is shown in the following code.
Dim xd As New XmlDocument() Dim xd As XmlNode xd.LoadXml("<XML>My Document</XML>") nd = xd.CreateNode(XmlNodeType.Element, "NEWNODE", "") nd.InnerText = "New Data" xd.FirstChild.AppendChild(nd)
Attributes Property
The Attributes property returns an XmlAttributesCollection that contains all the elements in the document.
DocumentNavigator Object
This object is a member of System.Xml
The DocumentNavigator enables you to navigate an XML document using a scrolling cursor model. It inherits from System.Xml.XmlNavigator, an abstract class.
Constructors
dn = New DocumentNavigator(xd As XmlDocument) dn = New DocumentNavigator(st As System.Xml.DocumentNavigator.NavState)
Properties
AttributeCount |
InnerText |
NamespaceURI |
ChildCount |
InnerXml |
NameTable |
HasAttributes |
IsDefault |
NodeType |
HasChildren |
IsEmptyTag |
OuterXml |
HasSelection |
IsReadOnly |
Prefix |
HasValue |
LocalName |
Selection |
IndexInParent |
Name |
Value |
Methods
Clone |
Move |
MoveToNext |
Compile |
MoveChildren |
MoveToNextAttribute |
CopyChildren |
MoveSelected |
MoveToNextSelected |
CopySelected |
MoveTo |
MoveToParent |
Equals |
MoveToAttribute |
MoveToPrevious |
Evaluate |
MoveToChild |
MoveToPreviousSelected |
Finalize |
MoveToDocument |
PopPosition |
GetAttribute |
MoveToDocumentElement |
PushPosition |
GetHashCode |
MoveToElement |
Remove |
GetNode |
MoveToFirst |
RemoveChildren |
GetType |
MoveToFirstAttribute |
RemoveSelected |
HasAttribute |
MoveToFirstChild |
Select |
Insert |
MoveToFirstSelected |
SelectSingle |
IsSamePosition |
MoveToId |
SetAttribute |
LookupPrefix |
MoveToLast |
ToString |
Matches |
MoveToLastChild |
|
MemberwiseClone |
MoveToLastSelected |
|
AttributeCount Property
The AttributeCount Property is an integer that represents the number of attributes associated with the current node. Note that the value of this property is predicated on the current node and will change as you traverse the document.