Прочитај на енглеском Уреди

Делите путем


XmlDeclaration.Encoding Property

Definition

Gets or sets the encoding level of the XML document.

C#
public string Encoding { get; set; }

Property Value

The valid character encoding name. The most commonly supported character encoding names for XML are the following:

Category Encoding Names
Unicode UTF-8, UTF-16
ISO 10646 ISO-10646-UCS-2, ISO-10646-UCS-4
ISO 8859 ISO-8859-n (where "n" is a digit from 1 to 9)
JIS X-0208-1997 ISO-2022-JP, Shift_JIS, EUC-JP

This value is optional. If a value is not set, this property returns String.Empty.

If an encoding attribute is not included, UTF-8 encoding is assumed when the document is written or saved out.

Examples

The following example creates an XmlDeclaration node and adds it to an XML document.

C#
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);
  }
}

Remarks

Unlike most XML attributes, encoding attribute values are not case-sensitive. This is because encoding character names follow ISO and Internet Assigned Numbers Authority (IANA) standards.

Applies to

Производ Верзије
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0