XmlDeclaration.Encoding 属性

定义

获取或设置 XML 文档的编码级别。Gets or sets the encoding level of the XML document.

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

属性值

String

有效的字符编码名称。The valid character encoding name. 受到最广泛支持的 XML 字符编码名称如下:The most commonly supported character encoding names for XML are the following:

类别Category 编码名Encoding Names
UnicodeUnicode UTF-8、UTF-16UTF-8, UTF-16
ISO 10646ISO 10646 ISO-10646-UCS-2、ISO-10646-UCS-4ISO-10646-UCS-2, ISO-10646-UCS-4
ISO 8859ISO 8859 ISO-8859-n(其中“n”表示从 1 到 9 的数字)ISO-8859-n (where "n" is a digit from 1 to 9)
JIS X-0208-1997JIS X-0208-1997 ISO-2022-JP、Shift_JIS、EUC-JPISO-2022-JP, Shift_JIS, EUC-JP

此值是可选的。This value is optional. 如果未设置值,该属性将返回 String.Empty。If a value is not set, this property returns String.Empty.

如果未包含编码特性,则在写出或保存文档时将假定为 UTF-8 编码。If an encoding attribute is not included, UTF-8 encoding is assumed when the document is written or saved out.

示例

下面的示例创建一个 XmlDeclaration 节点并将其添加到 XML 文档中。The following example creates an XmlDeclaration node and adds it to an XML document.

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   
   // Create and load the XML document.
   XmlDocument^ doc = gcnew XmlDocument;
   String^ xmlString = "<book><title>Oberon's Legacy</title></book>";
   doc->Load( gcnew StringReader( xmlString ) );
   
   // Create an XML declaration. 
   XmlDeclaration^ xmldecl;
   xmldecl = doc->CreateXmlDeclaration( "1.0", nullptr, nullptr );
   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 );
}

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 特性不同,编码特性值不区分大小写。Unlike most XML attributes, encoding attribute values are not case-sensitive. 这是因为编码字符名称遵循 ISO 和 Internet 分配的编号机构 (IANA) 标准。This is because encoding character names follow ISO and Internet Assigned Numbers Authority (IANA) standards.

适用于