XmlLinkedNode Class
Represents a node in the XML DOM that can be linked to other nodes.
Namespace: System.Xml
Assembly: System.Xml.dll
Inheritance
XmlLinkedNode
Syntax
public abstract class XmlLinkedNode : XmlNode
Remarks
The XmlLinkedNode
class is an abstract base class from which all nodes in the XML Document Object Model (DOM) that can be part of a linked list derive. This includes nodes such as XmlAttribute
, XmlCDataSection
, XmlComment
, XmlDeclaration
, XmlDocumentType
, XmlElement
, XmlEntity
, XmlEntityReference
, XmlIgnoredNamespaces
, XmlNameTable
, XmlNamespaceManager
, XmlNotation
, XmlProcessingInstruction
, XmlSignificantWhitespace
, XmlText
, and XmlWhitespace
. The linking mechanism allows for efficient traversal and manipulation of the XML tree structure.
This class provides properties for accessing the next and previous sibling nodes, facilitating operations like inserting, deleting, or reordering nodes within the DOM tree.
Properties
-
NextSibling
Gets the node immediately following this node.
- Return Value
- An
XmlLinkedNode
representing the next sibling node, ornull
if there is no next sibling. -
PreviousSibling
Gets the node immediately preceding this node.
- Return Value
- An
XmlLinkedNode
representing the previous sibling node, ornull
if there is no previous sibling.
Methods
-
AppendChild(XmlNode newChild)
Appends the specified node to the end of the list of children of this node.
- Parameters
newChild
- The
XmlNode
to append.
- Return Value
- The appended
XmlNode
. Returnsnull
if the operation fails. -
InsertAfter(XmlNode newChild, XmlNode refChild)
Inserts the specified node immediately after the specified reference node.
- Parameters
newChild
- The
XmlNode
to insert. refChild
- The
XmlNode
that is the reference node. ThenewChild
is inserted after this node.
- Return Value
- The inserted
XmlNode
. Returnsnull
if the operation fails. -
InsertBefore(XmlNode newChild, XmlNode refChild)
Inserts the specified node immediately before the specified reference node.
- Parameters
newChild
- The
XmlNode
to insert. refChild
- The
XmlNode
that is the reference node. ThenewChild
is inserted before this node.
- Return Value
- The inserted
XmlNode
. Returnsnull
if the operation fails. -
RemoveChild(XmlNode oldChild)
Removes the specified node from the list of children of this node.
- Parameters
oldChild
- The
XmlNode
to remove.
- Return Value
- The removed
XmlNode
. Returnsnull
if the operation fails. -
ReplaceChild(XmlNode newChild, XmlNode oldChild)
Replaces the specified node with the specified new node.
- Parameters
newChild
- The
XmlNode
to replace the existing node with. oldChild
- The
XmlNode
to replace.
- Return Value
- The node that was replaced. Returns
null
if the operation fails.
Examples
The following example demonstrates how to use the InsertAfter
method to insert a new element into an existing XML document.
using System;
using System.Xml;
public class XmlLinkedNodeExample
{
public static void Main(string[] args)
{
XmlDocument doc = new XmlDocument();
doc.LoadXml("<catalog>" +
" <book id='bk101'>" +
" <author>Gambardella, Matthew</author>" +
" <title>XML Developer's Guide</title>" +
" <genre>Computer</genre>" +
" <price>44.95</price>" +
" <publish_date>2000-10-01</publish_date>" +
" <description>An in-depth look at creating applications with XML.</description>" +
" </book>" +
" <book id='bk102'>" +
" <author>Ralls, Kim</author>" +
" <title>Midnight Rain</title>" +
" <genre>Fantasy</genre>" +
" <price>5.95</price>" +
" <publish_date>2000-12-16</publish_date>" +
" <description>A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.</description>" +
" </book>" +
"</catalog>");
XmlNode book1 = doc.SelectSingleNode("//book[@id='bk101']");
XmlNode authorNode = book1.SelectSingleNode("author");
// Create a new book element
XmlElement newBook = doc.CreateElement("book");
newBook.SetAttribute("id", "bk103");
newBook.InnerXml = "<author>New Author</author>" +
"<title>New Title</title>" +
"<genre>Fiction</genre>" +
"<price>19.99</price>" +
"<publish_date>2023-01-01</publish_date>" +
"<description>A captivating new story.</description>";
// Insert the new book after the first book
if (book1 != null && authorNode != null)
{
// Using InsertAfter with a reference node
// book1.InsertAfter(newBook, authorNode); // Incorrect - InsertAfter is on the parent node
// Correct usage: insert after the first book node itself
XmlNode catalogNode = doc.SelectSingleNode("catalog");
catalogNode.InsertAfter(newBook, book1);
Console.WriteLine("New book inserted successfully.");
Console.WriteLine(doc.OuterXml);
}
else
{
Console.WriteLine("Could not find the reference book node.");
}
}
}