共用方式為


XDeclaration.Encoding 屬性

定義

取得或設定這個文件的編碼方式。

public:
 property System::String ^ Encoding { System::String ^ get(); void set(System::String ^ value); };
public string Encoding { get; set; }
public string? Encoding { get; set; }
member this.Encoding : string with get, set
Public Property Encoding As String

屬性值

String

String,包含這個文件的字碼頁名稱。

範例

下列範例會建立兩個文件,其中一個編碼為 utf-8,而另一個編碼為 utf-16。 然後,會載入文件並將編碼方式列印至主控台。

Console.WriteLine("Creating a document with utf-8 encoding");  
XDocument encodedDoc8 = new XDocument(  
    new XDeclaration("1.0", "utf-8", "yes"),  
    new XElement("Root", "Content")  
);  
encodedDoc8.Save("EncodedUtf8.xml");  
Console.WriteLine("Encoding is:{0}", encodedDoc8.Declaration.Encoding);  
Console.WriteLine();  

Console.WriteLine("Creating a document with utf-16 encoding");  
XDocument encodedDoc16 = new XDocument(  
    new XDeclaration("1.0", "utf-16", "yes"),  
    new XElement("Root", "Content")  
);  
encodedDoc16.Save("EncodedUtf16.xml");  
Console.WriteLine("Encoding is:{0}", encodedDoc16.Declaration.Encoding);  
Console.WriteLine();  

XDocument newDoc8 = XDocument.Load("EncodedUtf8.xml");  
Console.WriteLine("Encoded document:");  
Console.WriteLine(File.ReadAllText("EncodedUtf8.xml"));  
Console.WriteLine();  
Console.WriteLine("Encoding of loaded document is:{0}", newDoc8.Declaration.Encoding);  
Console.WriteLine();  

XDocument newDoc16 = XDocument.Load("EncodedUtf16.xml");  
Console.WriteLine("Encoded document:");  
Console.WriteLine(File.ReadAllText("EncodedUtf16.xml"));  
Console.WriteLine();  
Console.WriteLine("Encoding of loaded document is:{0}", newDoc16.Declaration.Encoding);  
Console.WriteLine("Creating a document with utf-8 encoding")  
Dim encodedDoc8 As XDocument = _   
        <?xml version="1.0" encoding="utf-8" standalone="yes"?>  
            <Root>Content</Root>  
encodedDoc8.Save("EncodedUtf8.xml")  
Console.WriteLine("Encoding is:{0}", encodedDoc8.Declaration.Encoding)  
Console.WriteLine()  

Console.WriteLine("Creating a document with utf-16 encoding")  
Dim encodedDoc16 As XDocument = _   
        <?xml version="1.0" encoding="utf-16" standalone="yes"?>  
            <Root>Content</Root>  

encodedDoc16.Save("EncodedUtf16.xml")  
Console.WriteLine("Encoding is:{0}", encodedDoc16.Declaration.Encoding)  
Console.WriteLine()  

Dim newDoc8 As XDocument = XDocument.Load("EncodedUtf8.xml")  
Console.WriteLine("Encoded document:")  
Console.WriteLine(File.ReadAllText("EncodedUtf8.xml"))  
Console.WriteLine()  
Console.WriteLine("Encoding of loaded document is:{0}", newDoc8.Declaration.Encoding)  
Console.WriteLine()  

Dim newDoc16 As XDocument = XDocument.Load("EncodedUtf16.xml")  
Console.WriteLine("Encoded document:")  
Console.WriteLine(File.ReadAllText("EncodedUtf16.xml"))  
Console.WriteLine()  
Console.WriteLine("Encoding of loaded document is:{0}", newDoc16.Declaration.Encoding)  

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

Creating a document with utf-8 encoding  
Encoding is:utf-8  

Creating a document with utf-16 encoding  
Encoding is:utf-16  

Encoded document:  
<?xml version="1.0" encoding="utf-8" standalone="yes"?>  
<Root>Content</Root>  

Encoding of loaded document is:utf-8  

Encoded document:  
<?xml version="1.0" encoding="utf-16" standalone="yes"?>  
<Root>Content</Root>  

Encoding of loaded document is:utf-16  

備註

WebName 傳回的任何值都是有效的值。

如果您讀取編碼的檔,則此屬性會設定為字碼頁名稱。

如果您將此屬性設定為有效的字碼頁名稱,則在序列化時,LINQ to XML會使用指定的編碼序列化。

適用於

另請參閱