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 声明。 如果要指示文档是独立的,则必须使用此属性。 If you want to encode your document with an encoding other than utf-8, you can specify an encoding through the XDeclaration. 对文档进行编码的另一种方法是指定传递给 LINQ to XML 进行写入的编码 XmlWriter

适用于

另请参阅