XmlDocument.CreateElement 메서드

정의

XmlElement를 만듭니다.

오버로드

CreateElement(String)

지정된 이름을 가진 요소를 만듭니다.

CreateElement(String, String)

정규화된 이름과 NamespaceURI를 가진 XmlElement를 만듭니다.

CreateElement(String, String, String)

지정된 Prefix, LocalNameNamespaceURI가 있는 요소를 만듭니다.

CreateElement(String)

Source:
XmlDocument.cs
Source:
XmlDocument.cs
Source:
XmlDocument.cs

지정된 이름을 가진 요소를 만듭니다.

public:
 System::Xml::XmlElement ^ CreateElement(System::String ^ name);
public System.Xml.XmlElement CreateElement (string name);
member this.CreateElement : string -> System.Xml.XmlElement
Public Function CreateElement (name As String) As XmlElement

매개 변수

name
String

요소의 정규화된 이름입니다. 이름에 콜론이 포함되어 있는 경우 Prefix 속성은 콜론 앞의 이름 부분을 반영하고 LocalName 속성은 콜론 뒤의 이름 부분을 반영합니다. 정규화된 이름에는 'xmlns'라는 접두사가 포함될 수 없습니다.

반환

XmlElement입니다.

예제

다음 예제에서는 새 요소를 만들어 문서에 추가합니다.

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   
   //Create the XmlDocument.
   XmlDocument^ doc = gcnew XmlDocument;
   doc->LoadXml( "<book genre='novel' ISBN='1-861001-57-5'><title>Pride And Prejudice</title></book>" );
   
   //Create a new node and add it to the document.
   //The text node is the content of the price element.
   XmlElement^ elem = doc->CreateElement( "price" );
   XmlText^ text = doc->CreateTextNode( "19.95" );
   doc->DocumentElement->AppendChild( elem );
   doc->DocumentElement->LastChild->AppendChild( text );
   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()
  {
    //Create the XmlDocument.
    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" +
                "</book>");

    //Create a new node and add it to the document.
    //The text node is the content of the price element.
    XmlElement elem = doc.CreateElement("price");
    XmlText text = doc.CreateTextNode("19.95");
    doc.DocumentElement.AppendChild(elem);
    doc.DocumentElement.LastChild.AppendChild(text);

    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()
        'Create the XmlDocument.
        Dim doc As New XmlDocument()
        doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>"  & _
                    "<title>Pride And Prejudice</title>"  & _
                    "</book>")
        
        'Create a new node and add it to the document.
        'The text node is the content of the price element.
        Dim elem As XmlElement = doc.CreateElement("price")
        Dim text As XmlText = doc.CreateTextNode("19.95")
        doc.DocumentElement.AppendChild(elem)
        doc.DocumentElement.LastChild.AppendChild(text)
        
        Console.WriteLine("Display the modified XML...")
        doc.Save(Console.Out)
    End Sub
End Class

설명

반환된 인스턴스는 인터페이스를 XmlElement 구현하므로 기본 특성은 반환된 개체에 직접 만들어집니다.

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

W3C XML(Extensible Markup Language) 1.0 권장 사항에 따르면 EntityReference 노드가 특성 노드의 자식이 아닌 경우 문서 및 요소 노드 및 EntityReference 노드 내에서 요소 노드가 허용됩니다.

적용 대상

CreateElement(String, String)

Source:
XmlDocument.cs
Source:
XmlDocument.cs
Source:
XmlDocument.cs

정규화된 이름과 NamespaceURI를 가진 XmlElement를 만듭니다.

public:
 System::Xml::XmlElement ^ CreateElement(System::String ^ qualifiedName, System::String ^ namespaceURI);
public System.Xml.XmlElement CreateElement (string qualifiedName, string namespaceURI);
public System.Xml.XmlElement CreateElement (string qualifiedName, string? namespaceURI);
member this.CreateElement : string * string -> System.Xml.XmlElement
Public Function CreateElement (qualifiedName As String, namespaceURI As String) As XmlElement

매개 변수

qualifiedName
String

요소의 정규화된 이름입니다. 이름에 콜론이 포함되어 있는 경우, Prefix 속성은 콜론 앞의 이름 부분을 반영하고 LocalName 속성은 콜론 뒤의 이름 부분을 반영합니다. 정규화된 이름에는 'xmlns'라는 접두사가 포함될 수 없습니다.

namespaceURI
String

요소의 네임스페이스 URI입니다.

반환

XmlElement입니다.

설명

다음 C# 코드

XmlElement elem;
elem=doc.CreateElement("xy:item", "urn:abc");

는 다음 XML 텍스트와 동일한 요소를 생성합니다.

<xy:item
       xmlns:xy="urn:abc"/>

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

W3C XML(Extensible Markup Language) 1.0 권장 사항에 따르면 EntityReference 노드가 특성 노드의 자식이 아닌 경우 문서 및 요소 노드 및 EntityReference 노드 내에서 요소 노드가 허용됩니다.

적용 대상

CreateElement(String, String, String)

Source:
XmlDocument.cs
Source:
XmlDocument.cs
Source:
XmlDocument.cs

지정된 Prefix, LocalNameNamespaceURI가 있는 요소를 만듭니다.

public:
 virtual System::Xml::XmlElement ^ CreateElement(System::String ^ prefix, System::String ^ localName, System::String ^ namespaceURI);
public virtual System.Xml.XmlElement CreateElement (string prefix, string localName, string namespaceURI);
public virtual System.Xml.XmlElement CreateElement (string? prefix, string localName, string? namespaceURI);
abstract member CreateElement : string * string * string -> System.Xml.XmlElement
override this.CreateElement : string * string * string -> System.Xml.XmlElement
Public Overridable Function CreateElement (prefix As String, localName As String, namespaceURI As String) As XmlElement

매개 변수

prefix
String

새 요소의 접두사입니다. String.Empty와 null 은 같습니다.

localName
String

새 요소의 로컬 이름입니다.

namespaceURI
String

새 요소의 네임스페이스 URI입니다. String.Empty와 null 은 같습니다.

반환

XmlElement입니다.

예제

다음 예제에서는 기존 XML 문서에 새 요소를 추가합니다.

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   
   // Create the XmlDocument.
   XmlDocument^ doc = gcnew XmlDocument;
   String^ xmlData = "<book xmlns:bk='urn:samples'></book>";
   doc->Load( gcnew StringReader( xmlData ) );
   
   // Create a new element and add it to the document.
   XmlElement^ elem = doc->CreateElement( "bk", "genre", "urn:samples" );
   elem->InnerText = "fantasy";
   doc->DocumentElement->AppendChild( elem );
   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() {

    // Create the XmlDocument.
    XmlDocument doc = new XmlDocument();
    string xmlData = "<book xmlns:bk='urn:samples'></book>";

    doc.Load(new StringReader(xmlData));

    // Create a new element and add it to the document.
    XmlElement elem = doc.CreateElement("bk", "genre", "urn:samples");
    elem.InnerText = "fantasy";
    doc.DocumentElement.AppendChild(elem);

    Console.WriteLine("Display the modified XML...");
    doc.Save(Console.Out);
  }
}
Imports System.IO
Imports System.Xml

public class Sample 

  public shared sub Main() 

    ' Create the XmlDocument.
    Dim doc as XmlDocument = new XmlDocument()
    Dim xmlData as string = "<book xmlns:bk='urn:samples'></book>"

    doc.Load(new StringReader(xmlData))

    ' Create a new element and add it to the document.
    Dim elem as XmlElement = doc.CreateElement("bk", "genre", "urn:samples")
    elem.InnerText = "fantasy"
    doc.DocumentElement.AppendChild(elem)

    Console.WriteLine("Display the modified XML...")
    doc.Save(Console.Out)

  end sub
end class

설명

다음 C# 코드

XmlElement elem;
elem=doc.CreateElement("xy", "item", "urn:abc");

는 다음 XML 텍스트에 해당하는 요소를 만듭니다.

<xy:item xmlns:xy="urn:abc"/>

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

W3C XML(Extensible Markup Language) 1.0 권장 사항에 따라 EntityReference가 특성 노드 외부에 있는 경우 문서 및 요소 노드 및 EntityReference 노드 내에서 요소 노드가 허용됩니다.

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

적용 대상