XmlDeclaration.Encoding Propiedad

Definición

Obtiene o establece el nivel de codificación del documento 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

Valor de propiedad

String

El nombre de codificación de caracteres válidos. Los nombres de codificación de caracteres que presentan mayor compatibilidad en documentos XML son los siguientes:

Category Nombres de codificación
Unicode UTF-8, UTF-16
ISO 10646 ISO-10646-UCS-2, ISO-10646-UCS-4
ISO 8859 ISO-8859-n (donde "n" es un dígito de 1 a 9)
JIS X-0208-1997 ISO-2022-JP, Shift_JIS, EUC-JP

Este valor es opcional. Si no se establece un valor, esta propiedad devuelve String.Empty.

Si no se incluye un atributo de codificación, se supone la codificación UTF-8 cuando el documento se escribe o se guarda.

Ejemplos

En el ejemplo siguiente se crea un XmlDeclaration nodo y se agrega a un documento XML.

#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

Comentarios

A diferencia de la mayoría de los atributos XML, los valores de atributo de codificación no distinguen mayúsculas de minúsculas. Esto se debe a que los nombres de caracteres de codificación siguen los estándares ISO e Internet Assigned Numbers Authority (IANA).

Se aplica a