XObject.AddAnnotation(Object) 方法

定義

將物件加入此 XObject 的註釋清單。Adds an object to the annotation list of this 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

物件,包含要加入的註釋。An object that contains the annotation to add.

範例

下列範例會將批註加入至 XElementThe following example adds an annotation to an 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  

這個範例會產生下列輸出:This example produces the following output:

T1  

備註

請注意,批註不是資訊集的一部分;它們不會保存或顯示 ToStringNote that annotations are not part of the infoset; they are not persisted, or displayed by ToString. 此外,如果您將 XML 命名空間匯入至 VB 專案,並使用 System.xml.linq.saveoptions> OmitDuplicateNamespaces 列舉值來呼叫 AddAnnotation,則只會有一個元素包含 XML 命名空間屬性,而不是每個元素。Also, if you import an XML Namespace into your VB project and call AddAnnotation with the SaveOptions.OmitDuplicateNamespaces enumeration value, then only one element will contain the XML Namespace attribute instead of every element. 如需詳細資訊,請參閱 在 XML 常值中移除重複的命名空間For more information, see Removing duplicate namespaces in XML Literals.

適用於

另請參閱