XObject.Annotations Method

Definition

Overloads

Annotations(Type)

Gets a collection of annotations of the specified type for this XObject.

Annotations<T>()

Gets a collection of annotations of the specified type for this XObject.

Annotations(Type)

Source:
XObject.cs
Source:
XObject.cs
Source:
XObject.cs

Gets a collection of annotations of the specified type for this XObject.

C#
public System.Collections.Generic.IEnumerable<object> Annotations(Type type);

Parameters

type
Type

The type of the annotations to retrieve.

Returns

An IEnumerable<T> of Object that contains the annotations that match the specified type for this XObject.

Examples

The following example adds some annotations to an XElement, then retrieves a collection of annotations by using this method.

C#
public class MyAnnotation  
{  
    private string tag;  
    public string Tag { get { return tag; } set { tag = value; } }  
    public MyAnnotation(string tag)  
    {  
        this.tag = tag;  
    }  
}  

class Program  
{  
    static void Main(string[] args)  
    {  
        XElement root = new XElement("Root", "content");  
        root.AddAnnotation(new MyAnnotation("T1"));  
        root.AddAnnotation(new MyAnnotation("T2"));  
        root.AddAnnotation("abc");  
        root.AddAnnotation("def");  

        IEnumerable<object> annotationList;  
        annotationList = root.Annotations(typeof(MyAnnotation));  
        foreach (object ma in annotationList)  
            Console.WriteLine(((MyAnnotation)ma).Tag);  
        Console.WriteLine("----");  

        IEnumerable<object> stringAnnotationList;  
        stringAnnotationList = root.Annotations(typeof(string));  
        foreach (object str in stringAnnotationList)  
            Console.WriteLine((string)str);  
    }  
}  

This example produces the following output:

T1  
T2  
----  
abc  
def  

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Annotations<T>()

Source:
XObject.cs
Source:
XObject.cs
Source:
XObject.cs

Gets a collection of annotations of the specified type for this XObject.

C#
public System.Collections.Generic.IEnumerable<T> Annotations<T>() where T : class;

Type Parameters

T

The type of the annotations to retrieve.

Returns

An IEnumerable<T> that contains the annotations for this XObject.

Examples

The following example uses this method to retrieve annotations on an element.

C#
public class MyAnnotation {  
    private string tag;  
    public string Tag {get{return tag;} set{tag=value;}}  
    public MyAnnotation(string tag) {  
        this.tag = tag;  
    }  
}  

class Program {  
    static void Main(string[] args) {     
        XElement root = new XElement("Root", "content");  
        root.AddAnnotation(new MyAnnotation("T1"));  
        root.AddAnnotation(new MyAnnotation("T2"));  
        root.AddAnnotation("abc");  
        root.AddAnnotation("def");  

        IEnumerable<MyAnnotation> annotationList;  
        annotationList = root.Annotations<MyAnnotation>();  
        foreach (MyAnnotation ma in annotationList)  
            Console.WriteLine(ma.Tag);  
        Console.WriteLine("----");  

        IEnumerable<string> stringAnnotationList;  
        stringAnnotationList = root.Annotations<string>();  
        foreach (string str in stringAnnotationList)  
            Console.WriteLine(str);  
    }  
}  

This example produces the following output:

T1  
T2  
----  
abc  
def  

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0