XmlDocument.CreateElement 메서드
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
XmlElement를 만듭니다.
오버로드
| Name | Description |
|---|---|
| CreateElement(String) |
지정된 이름을 가진 요소를 만듭니다. |
| CreateElement(String, String) |
XmlElement 정규화된 이름과 NamespaceURI.을 사용하여 만듭니다. |
| CreateElement(String, String, String) |
지정된 PrefixLocalNameNamespaceURI및 을 사용하여 요소를 만듭니다. |
CreateElement(String)
지정된 이름을 가진 요소를 만듭니다.
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;
using System.Xml;
public class Sample
{
public static void CreateTextNodeExample()
{
// Create the XmlDocument.
XmlDocument doc = new();
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);
}
}
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
이 예제에서는 다음 출력을 생성합니다.
Display the modified XML...
<?xml version="1.0" encoding="utf-8"?>
<book genre="novel" ISBN="1-861001-57-5">
<title>Pride And Prejudice</title>
<price>19.95</price>
</book>
설명
반환된 인스턴스는 인터페이스를 XmlElement 구현하므로 기본 특성은 반환된 개체에 직접 만들어집니다.
이 메서드는 문서의 컨텍스트에서 새 개체를 만들지만 문서 트리에 새 개체를 자동으로 추가하지는 않습니다. 새 개체를 추가하려면 노드 삽입 메서드 중 하나를 명시적으로 호출해야 합니다.
W3C XML(Extensible Markup Language) 1.0 권장 사항에 따라 EntityReference 노드가 특성 노드의 자식이 아닌 경우 문서 및 요소 노드 및 EntityReference 노드에서 요소 노드가 허용됩니다.
적용 대상
CreateElement(String, String)
XmlElement 정규화된 이름과 NamespaceURI.을 사용하여 만듭니다.
public:
System::Xml::XmlElement ^ CreateElement(System::String ^ qualifiedName, System::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)
지정된 PrefixLocalNameNamespaceURI및 을 사용하여 요소를 만듭니다.
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);
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;
using System.IO;
using System.Xml;
public class Sample1
{
public static void CreateElementExample()
{
// Create the XmlDocument.
XmlDocument doc = new();
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 확장입니다.