Freigeben über


XmlDeclaration.Encoding-Eigenschaft

Ruft die Codierungsebene des XML-Dokuments ab oder legt diese fest.

Namespace: System.Xml
Assembly: System.Xml (in system.xml.dll)

Syntax

'Declaration
Public Property Encoding As String
'Usage
Dim instance As XmlDeclaration
Dim value As String

value = instance.Encoding

instance.Encoding = value
public string Encoding { get; set; }
public:
property String^ Encoding {
    String^ get ();
    void set (String^ value);
}
/** @property */
public String get_Encoding ()

/** @property */
public void set_Encoding (String value)
public function get Encoding () : String

public function set Encoding (value : String)

Eigenschaftenwert

Der gültige Name der Zeichencodierung. Im Folgenden werden die am häufigsten unterstützten Namen für die Zeichencodierung in XML aufgeführt:

Kategorie

Codierungsnamen

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 angegeben ist, wird beim Schreiben oder Speichern des Dokuments die UTF-8-Codierung zugrunde gelegt.

Hinweise

Im Gegensatz zu den meisten XML-Attributen wird bei den Werten von Codierungsattributen die Groß- und Kleinschreibung nicht berücksichtigt. Das liegt daran, dass Zeichencodierungsnamen dem ISO-Standard und dem IANA-Standard (Internet Assigned Numbers Authority) entsprechen.

Beispiel

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

Imports System
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
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);
      
  }
}
#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 );
}
import System.*;
import System.IO.*;
import System.Xml.*;

public class Sample
{
    public static void main(String[] args)
    {
        // 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.set_Encoding("UTF-8");
        xmlDecl.set_Standalone("yes");

        // Add the new node to the document.
        XmlElement root = doc.get_DocumentElement();
        doc.InsertBefore(xmlDecl, root);

        // Display the modified XML document 
        Console.WriteLine(doc.get_OuterXml());
    } //main
} //Sample

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0, 1.0

Siehe auch

Referenz

XmlDeclaration-Klasse
XmlDeclaration-Member
System.Xml-Namespace