XmlDeclaration.Encoding 속성

정의

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

속성 값

String

올바른 문자 인코딩 이름입니다. 다음은 가장 일반적으로 지원되는 XML 문자 인코딩 이름입니다.

범주 인코딩 이름
유니코드(Unicode) UTF-8, UTF-16
ISO 10646 ISO-10646-UCS-2, ISO-10646-UCS-4
ISO 8859 ISO-8859-n(여기서 "n"은 1에서 9 사이의 숫자임)
JIS X-0208-1997 ISO-2022-JP, Shift_JIS, EUC-JP

이 값은 선택 사항입니다. 값이 설정되지 않으면 이 속성은 String.Empty를 반환합니다.

인코딩 속성이 포함되지 않으면 문서를 쓰거나 저장할 때 UTF-8 인코딩을 가정합니다.

예제

다음 예제에서는 노드를 XmlDeclaration 만들고 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

설명

대부분의 XML 특성과 달리 인코딩 특성 값은 대/소문자를 구분하지 않습니다. 인코딩 문자 이름은 ISO 및 IANA(Internet Assigned Numbers Authority) 표준을 따르기 때문입니다.

적용 대상