Export (0) Print
Expand All

XmlDocument.CreateElement Method

Creates an XmlElement.

Overload List

Creates an element with the specified name.

Supported by the .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;

Creates an XmlElement with the qualified name and NamespaceURI.

Supported by the .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;

Creates an element with the specified Prefix, LocalName, and NamespaceURI.

Supported by the .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;

Example

[Visual Basic, C#, C++] The following example adds a new element to the existing XML document.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of CreateElement. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
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] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

XmlDocument Class | XmlDocument Members | System.Xml Namespace

Show:
© 2015 Microsoft