XmlDocument.CreateXmlDeclaration(String, String, String) 메서드

정의

지정된 값이 있는 XmlDeclaration 노드를 만듭니다.

public:
 virtual System::Xml::XmlDeclaration ^ CreateXmlDeclaration(System::String ^ version, System::String ^ encoding, System::String ^ standalone);
public virtual System.Xml.XmlDeclaration CreateXmlDeclaration (string version, string encoding, string standalone);
public virtual System.Xml.XmlDeclaration CreateXmlDeclaration (string version, string? encoding, string? standalone);
abstract member CreateXmlDeclaration : string * string * string -> System.Xml.XmlDeclaration
override this.CreateXmlDeclaration : string * string * string -> System.Xml.XmlDeclaration
Public Overridable Function CreateXmlDeclaration (version As String, encoding As String, standalone As String) As XmlDeclaration

매개 변수

version
String

버전은 "1.0"이어야 합니다.

encoding
String

인코딩 특성 값입니다. XmlDocument를 파일이나 스트림으로 저장할 경우 사용하는 인코딩입니다. 그러므로 Encoding 클래스에서 지원하는 문자열로 설정되어야 합니다. 그렇지 않으면 Save(String)이 실패합니다. null 또는 String.Empty일 경우 Save 메서드에서 인코딩 특성을 XML 선언에 기록하지 않으므로 기본 인코딩인 UTF-8을 사용하게 됩니다.

참고: XmlDocumentTextWriterXmlTextWriter로 저장하면 이 인코딩 값이 삭제됩니다. 대신 TextWriter 또는 XmlTextWriter 의 인코딩을 사용합니다. 그러면 기록된 XML을 올바른 인코딩을 사용하여 다시 읽을 수 있습니다.

standalone
String

값은 "Yes" 또는 "No"여야 합니다. 값이 null이나 String.Empty일 경우에는 Save 메서드에서 독립형 특성을 XML 선언에 기록하지 않습니다.

반환

XmlDeclaration 노드입니다.

예외

version 또는 standalone의 값이 위에서 지정한 값 이외의 값입니다.

예제

다음 예제에서는 XML 선언을 만들고 문서에 추가합니다.

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   XmlDocument^ doc = gcnew XmlDocument;
   doc->LoadXml( "<book genre='novel' ISBN='1-861001-57-5'><title>Pride And Prejudice</title></book>" );
   
   //Create an XML declaration. 
   XmlDeclaration^ xmldecl;
   xmldecl = doc->CreateXmlDeclaration( "1.0", nullptr, nullptr );
   
   //Add the new node to the document.
   XmlElement^ root = doc->DocumentElement;
   doc->InsertBefore( xmldecl, root );
   Console::WriteLine( "Display the modified XML..." );
   doc->Save( Console::Out );
}

using System;
using System.IO;
using System.Xml;

public class Sample
{
  public static void Main()
  {
    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" +
                "</book>");

    //Create an XML declaration.
    XmlDeclaration xmldecl;
    xmldecl = doc.CreateXmlDeclaration("1.0",null,null);

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

    Console.WriteLine("Display the modified XML...");
    doc.Save(Console.Out);
  }
}
Option Explicit
Option Strict

Imports System.IO
Imports System.Xml

Public Class Sample
    
    Public Shared Sub Main()
        Dim doc As New XmlDocument()
        doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" & _
                    "<title>Pride And Prejudice</title>" & _
                    "</book>")
        
        'Create an XML declaration. 
        Dim xmldecl As XmlDeclaration
        xmldecl = doc.CreateXmlDeclaration("1.0", Nothing, Nothing)
        
        'Add the new node to the document.
        Dim root As XmlElement = doc.DocumentElement
        doc.InsertBefore(xmldecl, root)
        
        Console.WriteLine("Display the modified XML...")
        doc.Save(Console.Out)
    End Sub
End Class

설명

특성은 노드가 아닌 노드에서 XmlDeclaration 특수 속성으로 XmlAttribute 노출됩니다.

이 메서드는 문서의 컨텍스트에서 새 개체를 만들지만 문서 트리에 새 개체를 자동으로 추가하지는 않습니다. 새 개체를 추가하려면 노드 삽입 메서드 중 하나를 명시적으로 호출해야 합니다.

W3C XML(Extensible Markup Language) 1.0 권장 사항에 따라 노드는 XmlDeclaration 문서의 첫 번째 노드여야 합니다.

이 메서드는 DOM(문서 개체 모델)에 대한 Microsoft 확장입니다.

적용 대상

추가 정보