XmlDocument.CreateElement メソッド

定義

XmlElement を作成します。

オーバーロード

CreateElement(String)

指定した名前を使用して要素を作成します。

CreateElement(String, String)

限定名と NamespaceURI を使用して XmlElement を作成します。

CreateElement(String, String, String)

指定した PrefixLocalName、および NamespaceURI を使用して、要素を作成します。

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) 1.0 の推奨事項に従って、Element ノードは Document ノードと Element ノード内で許可され、EntityReference ノードが Attribute ノードの子でない場合は 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) 1.0 の推奨事項に従って、Element ノードは Document ノードと Element ノード内で許可され、EntityReference ノードが Attribute ノードの子でない場合は EntityReference ノードで許可されます。

適用対象

CreateElement(String, String, String)

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

指定した PrefixLocalName、および NamespaceURI を使用して、要素を作成します。

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 Extensible Markup Language (XML) 1.0 の推奨事項に従って、Element ノードは Document ノードと Element ノード内で許可され、EntityReference ノードが Attribute ノードの外部にある場合は EntityReference ノードで許可されます。

このメソッドは、ドキュメント オブジェクト モデル (DOM) のMicrosoft拡張機能です。

適用対象