XObject.AddAnnotation(Object) 方法

定義

將物件加入此 XObject 的註釋清單。

public:
 void AddAnnotation(System::Object ^ annotation);
public void AddAnnotation (object annotation);
member this.AddAnnotation : obj -> unit
Public Sub AddAnnotation (annotation As Object)

參數

annotation
Object

物件,包含要加入的註釋。

範例

下列範例會將批註新增至 XElement

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

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

        MyAnnotation ma2 = (MyAnnotation)root.Annotation<MyAnnotation>();  
        Console.WriteLine(ma2.Tag);  
    }  
}  
Public Class MyAnnotation  
    Private _tag As String  

    Property Tag() As String  
        Get  
            Return Me._tag  
        End Get  
        Set(ByVal Value As String)  
            Me._tag = Value  
        End Set  
    End Property  

    Public Sub New(ByVal tag As String)  
        Me._tag = tag  
    End Sub  
End Class  

Module Module1  
    Sub Main()  
        Dim ma As MyAnnotation = New MyAnnotation("T1")  
        Dim root As XElement = <Root>content</Root>  
        root.AddAnnotation(ma)  

        Dim ma2 As MyAnnotation = DirectCast(root.Annotation(Of MyAnnotation)(), MyAnnotation)  
        Console.WriteLine(ma2.Tag)  
    End Sub  

End Module  

這個範例會產生下列輸出:

T1  

備註

請注意,批註不是資訊集的一部分;它們不會保存或由 ToString 顯示。 此外,如果您將 XML 命名空間匯入VB專案,並使用 SaveOptions.OmitDuplicateNamespaces 列舉值呼叫 AddAnnotation,則只有一個專案會包含 XML 命名空間屬性,而不是每個元素。 如需詳細資訊,請參閱 移除 XML 常值中的重複命名空間

適用於

另請參閱