XmlDeclaration.Encoding 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定 XML 文件的編碼層級。
public:
property System::String ^ Encoding { System::String ^ get(); void set(System::String ^ value); };
public string Encoding { get; set; }
member this.Encoding : string with get, set
Public Property Encoding As String
屬性值
有效的字元編碼名稱。 XML 最常支援的字元編碼名稱如下:
| 分類 | 編碼名稱 |
|---|---|
| Unicode | UTF-8、UTF-16 |
| ISO 10646 | ISO-10646-UCS-2,ISO-10646-UCS-4 |
| ISO 8859 | ISO-8859-n(其中「n」為 1 到 9 的數字) |
| JIS X-0208-1997 | ISO-2022-JP,Shift_JIS,EUC-JP |
此為選用值。 若值未被設定,此屬性會回傳 String.Empty。
若未包含編碼屬性,撰寫或儲存文件時會假設採用 UTF-8 編碼。
範例
以下範例建立一個 XmlDeclaration 節點並將其加入 XML 文件中。
using System;
using System.IO;
using System.Xml;
public class Sample {
public static void Main() {
// Create and load the XML document.
XmlDocument doc = new XmlDocument();
string xmlString = "<book><title>Oberon's Legacy</title></book>";
doc.Load(new StringReader(xmlString));
// Create an XML declaration.
XmlDeclaration xmldecl;
xmldecl = doc.CreateXmlDeclaration("1.0",null,null);
xmldecl.Encoding="UTF-8";
xmldecl.Standalone="yes";
// Add the new node to the document.
XmlElement root = doc.DocumentElement;
doc.InsertBefore(xmldecl, root);
// Display the modified XML document
Console.WriteLine(doc.OuterXml);
}
}
Imports System.IO
Imports System.Xml
public class Sample
public shared sub Main()
' Create and load the XML document.
Dim doc as XmlDocument = new XmlDocument()
Dim xmlString as string = "<book><title>Oberon's Legacy</title></book>"
doc.Load(new StringReader(xmlString))
' Create an XML declaration.
Dim xmldecl as XmlDeclaration
xmldecl = doc.CreateXmlDeclaration("1.0",nothing, nothing)
xmldecl.Encoding="UTF-8"
xmldecl.Standalone="yes"
' Add the new node to the document.
Dim root as XmlElement = doc.DocumentElement
doc.InsertBefore(xmldecl, root)
' Display the modified XML document
Console.WriteLine(doc.OuterXml)
end sub
end class
備註
與大多數 XML 屬性不同,編碼屬性值不區分大小寫。 這是因為編碼字元名稱遵循 ISO 及網際網路號碼授權機構(IANA)標準。