XDocument.Declaration 屬性

定義

取得或設定這個文件的 XML 宣告。

public:
 property System::Xml::Linq::XDeclaration ^ Declaration { System::Xml::Linq::XDeclaration ^ get(); void set(System::Xml::Linq::XDeclaration ^ value); };
public System.Xml.Linq.XDeclaration Declaration { get; set; }
public System.Xml.Linq.XDeclaration? Declaration { get; set; }
member this.Declaration : System.Xml.Linq.XDeclaration with get, set
Public Property Declaration As XDeclaration

屬性值

XDeclaration

XDeclaration,包含這個文件的 XML 宣告。

範例

下列範例會使用這個屬性來擷取檔的 XML 宣告。

XDocument doc = new XDocument(  
    new XDeclaration("1.0", "utf-8", "yes"),  
    new XComment("This is a comment"),  
    new XElement("Root", "content")  
);  

Console.WriteLine(doc.Declaration);  
Dim doc As XDocument = _  
    <?xml version="1.0" encoding="utf-8" standalone="yes"?>  
        <!--This is a comment-->  
        <Root>content</Root>  

Console.WriteLine(doc.Declaration)  

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

<?xml version="1.0" encoding="utf-8" standalone="yes"?>  

備註

有時候您必須建立檔的 XML 宣告。 如果您想要指出檔是獨立檔,則必須使用這個屬性。 如果您想要使用 utf-8 以外的編碼方式來編碼檔,您可以透過 XDeclaration 指定編碼方式。 另一種編碼檔的方法,是指定您傳遞至寫入LINQ to XML的 編碼 XmlWriter 方式。

適用於

另請參閱