XObject.AddAnnotation(Object) 方法

定義

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

C#
public void AddAnnotation (object annotation);

參數

annotation
Object

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

範例

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

C#
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);  
    }  
}  

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

T1  

備註

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

適用於

產品 版本
.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
.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
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

另請參閱