다음을 통해 공유


XmlDocument.CreateXmlDeclaration 메서드

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

네임스페이스: System.Xml
어셈블리: System.Xml(system.xml.dll)

구문

‘선언
Public Overridable Function CreateXmlDeclaration ( _
    version As String, _
    encoding As String, _
    standalone As String _
) As XmlDeclaration
‘사용 방법
Dim instance As XmlDocument
Dim version As String
Dim encoding As String
Dim standalone As String
Dim returnValue As XmlDeclaration

returnValue = instance.CreateXmlDeclaration(version, encoding, standalone)
public virtual XmlDeclaration CreateXmlDeclaration (
    string version,
    string encoding,
    string standalone
)
public:
virtual XmlDeclaration^ CreateXmlDeclaration (
    String^ version, 
    String^ encoding, 
    String^ standalone
)
public XmlDeclaration CreateXmlDeclaration (
    String version, 
    String encoding, 
    String standalone
)
public function CreateXmlDeclaration (
    version : String, 
    encoding : String, 
    standalone : String
) : XmlDeclaration

매개 변수

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

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

  • standalone
    값은 "yes" 또는 "no"여야 합니다. 값이 Null 참조(Visual Basic의 경우 Nothing)이나 String.Empty일 경우에는 Save 메서드에서 독립형 특성을 XML 선언에 기록하지 않습니다.

반환 값

XmlDeclaration 노드입니다.

예외

예외 형식 조건

ArgumentException

version 또는 standalone의 값이 위에서 지정한 값 이외의 다른 값인 경우

설명

특성은 XmlDeclaration 노드의 특수 속성으로 노출되며, XmlAttribute 노드로 노출되지 않습니다.

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

W3C XML(Extensible Markup Language) 1.0 권장 사항(www.w3.org/TR/1998/REC-xml-19980210)에 따르면 XmlDeclaration 노드는 문서의 첫 번째 노드여야 합니다.

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

예제

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

Option Explicit
Option Strict

Imports System
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 'Main
End Class 'Sample
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);
  }
}
#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 );
}
import System.*;
import System.IO.*;
import System.Xml.*;

public class Sample
{
    public static void main(String[] args)
    {
        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.get_DocumentElement();
        doc.InsertBefore(xmlDecl, root);

        Console.WriteLine("Display the modified XML...");
        doc.Save(Console.get_Out());
    } //main
} //Sample

플랫폼

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

.NET Framework에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.

버전 정보

.NET Framework

2.0, 1.1, 1.0에서 지원

.NET Compact Framework

2.0, 1.0에서 지원

참고 항목

참조

XmlDocument 클래스
XmlDocument 멤버
System.Xml 네임스페이스
XmlDeclaration 클래스