XmlLinkedNode Class

Represents a node in the XML DOM that can be linked to other nodes.

Namespace: System.Xml

Assembly: System.Xml.dll

Inheritance

Object

XmlNode

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

Methods

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.");
        }
    }
}