XmlDeclaration.Encoding Eigenschaft

Definition

Dient zum Abrufen oder Festlegen der Codierungsebene des XML-Dokuments.

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

Eigenschaftswert

Der gültige Zeichencodierungsname. Die am häufigsten unterstützten Zeichencodierungsnamen für XML sind die folgenden:

Kategorie Codieren von Namen
Unicode UTF-8, UTF-16
ISO 10646 ISO-10646-UCS-2, ISO-10646-UCS-4
ISO 8859 ISO-8859-n (wobei "n" eine Ziffer von 1 bis 9 ist)
JIS X-0208-1997 ISO-2022-JP, Shift_JIS, EUC-JP

Dieser Wert ist optional. Wenn kein Wert festgelegt ist, gibt diese Eigenschaft String.Empty zurück.

Wenn kein Codierungsattribut enthalten ist, wird die UTF-8-Codierung angenommen, wenn das Dokument geschrieben oder gespeichert wird.

Beispiele

Im folgenden Beispiel wird ein XmlDeclaration Knoten erstellt und einem XML-Dokument hinzugefügt.

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

Hinweise

Im Gegensatz zu den meisten XML-Attributen wird bei Codierungsattributen die Groß-/Kleinschreibung nicht beachtet. Dies liegt daran, dass Codierungszeichennamen ISO- und Internet Assigned Numbers Authority (IANA)-Standards entsprechen.

Gilt für: