次の方法で共有


XmlDocument.CreateElement メソッド

XmlElement を作成します。

オーバーロードの一覧

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

.NET Compact Framework でもサポート。

[Visual Basic] Overloads Public Function CreateElement(String) As XmlElement

[C#] public XmlElement CreateElement(string);

[C++] public: XmlElement* CreateElement(String*);

[JScript] public function CreateElement(String) : XmlElement;

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

.NET Compact Framework でもサポート。

[Visual Basic] Overloads Public Function CreateElement(String, String) As XmlElement

[C#] public XmlElement CreateElement(string, string);

[C++] public: XmlElement* CreateElement(String*, String*);

[JScript] public function CreateElement(String, String) : XmlElement;

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

.NET Compact Framework でもサポート。

[Visual Basic] Overloads Public Overridable Function CreateElement(String, String, String) As XmlElement

[C#] public virtual XmlElement CreateElement(string, string, string);

[C++] public: virtual XmlElement* CreateElement(String*, String*, String*);

[JScript] public function CreateElement(String, String, String) : XmlElement;

使用例

[Visual Basic, C#, C++] 新しい要素を既存の XML ドキュメントに追加する例を次に示します。

[Visual Basic, C#, C++] メモ   ここでは、CreateElement のオーバーロード形式のうちの 1 つだけについて、使用例を示します。その他の例については、各オーバーロード形式のトピックを参照してください。

 
Imports System
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#] 
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);

  }
}

[C++] 
#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;

int main()
{
    // Create the XmlDocument.
    XmlDocument* doc = new XmlDocument();
    String* xmlData = S"<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(S"bk", S"genre", S"urn:samples");
    elem->InnerText = S"fantasy";
    doc->DocumentElement->AppendChild(elem);

    Console::WriteLine(S"Display the modified XML...");
    doc->Save(Console::Out);
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

参照

XmlDocument クラス | XmlDocument メンバ | System.Xml 名前空間