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
| Constructor | Summary |
|---|---|
XObject() | Initializes a new instance of the XObject class. |
Properties
| Property | Type | Access | Summary |
|---|---|---|---|
Parent | XContainer | protected internal | The parent node of this object. |
Document | XDocument | protected internal | The document that contains this object. |
Methods
| Method | Return Type | Summary |
|---|---|---|
Annotation(Type type) | object | Gets the first annotation of the specified type. |
Annotation<T>() | T | Gets the first annotation of the specified generic type. |
AddAnnotation(object annotation) | void | Associates an object with this node as an annotation. |
RemoveAnnotations<T>() | void | Removes 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>());