Microsoft Docs XObject Class

XObject Class

Overview

The XObject class serves as the abstract base class for the XNode and XAttribute types. It provides functionality common to all XML LINQ objects such as annotation support, change notification, and parent handling.

Namespace

System.Xml.Linq

Assembly

System.Xml.Linq.dll

Inheritance

Object → XObject

Syntax

public abstract class XObject
{
    protected XObject();
    public object Annotation(Type type);
    public T Annotation<T>();
    public void AddAnnotation(object annotation);
    public void RemoveAnnotations<T>();
    internal XContainer Parent { get; }
    internal XDocument Document { get; }
}

Constructors

ConstructorSummary
XObject()Initializes a new instance of the XObject class.

Properties

PropertyTypeAccessSummary
ParentXContainerprotected internalThe parent node of this object.
DocumentXDocumentprotected internalThe document that contains this object.

Methods

MethodReturn TypeSummary
Annotation(Type type)objectGets the first annotation of the specified type.
Annotation<T>()TGets the first annotation of the specified generic type.
AddAnnotation(object annotation)voidAssociates an object with this node as an annotation.
RemoveAnnotations<T>()voidRemoves all annotations of a specified type from this node.

Remarks

Annotations provide a way to attach custom data to an XObject without modifying the underlying XML. The annotation methods are thread‑safe for read‑only access but are not safe for concurrent writes.

Examples

// Create an XML tree and add an annotation to an element
XElement root = new XElement("Root");
root.AddAnnotation(new Guid("d13a3f6b-2c0e-4b3b-9c6c-8a9e5c1b3c44"));
Console.WriteLine(root.Annotation<Guid>());

See Also