Ask Learn
Preview
Please sign in to use this experience.
Sign inThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Creates an XmlElement.
CreateElement(String) |
Creates an element with the specified name. |
CreateElement(String, String) |
Creates an XmlElement with the qualified name and NamespaceURI. |
CreateElement(String, String, String) |
Creates an element with the specified Prefix, LocalName, and NamespaceURI. |
Creates an element with the specified name.
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
The qualified name of the element. If the name contains a colon then the Prefix property reflects the part of the name preceding the colon and the LocalName property reflects the part of the name after the colon. The qualified name cannot include a prefix of 'xmlns'.
The new XmlElement
.
The following example creates a new element and adds it to the document.
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
Note that the instance returned implements the XmlElement
interface, so default attributes would be created directly on the returned object.
Although this method creates the new object in the context of the document, it does not automatically add the new object to the document tree. To add the new object, you must explicitly call one of the node insert methods.
According to the W3C Extensible Markup Language (XML) 1.0 recommendation, Element nodes are allowed within Document and Element nodes, and in EntityReference nodes when the EntityReference node is not a child of an Attribute node.
Product | Versions |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 2.0, 2.1 |
UWP | 10.0 |
Creates an XmlElement with the qualified name and NamespaceURI.
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
The qualified name of the element. If the name contains a colon then the Prefix property will reflect the part of the name preceding the colon and the LocalName property will reflect the part of the name after the colon. The qualified name cannot include a prefix of 'xmlns'.
The namespace URI of the element.
The new XmlElement
.
The following C# code:
XmlElement elem;
elem=doc.CreateElement("xy:item", "urn:abc");
...results in an element that's equivalent to the following XML.
<xy:item
xmlns:xy="urn:abc"/>
Although this method creates the new object in the context of the document, it does not automatically add the new object to the document tree. To add the new object, you must explicitly call one of the node insert methods.
According to the W3C Extensible Markup Language (XML) 1.0 recommendation, Element nodes are allowed within Document and Element nodes, and in EntityReference nodes when the EntityReference node is not a child of an Attribute node.
Product | Versions |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 2.0, 2.1 |
UWP | 10.0 |
Creates an element with the specified Prefix, LocalName, and 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
The prefix of the new element (if any). String.Empty and null
are equivalent.
The local name of the new element.
The namespace URI of the new element (if any). String.Empty and null
are equivalent.
The new XmlElement.
The following example adds a new element to the existing XML document.
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
The following C# code:
XmlElement elem;
elem=doc.CreateElement("xy", "item", "urn:abc");
...creates an element equivalent to the following XML:
<xy:item xmlns:xy="urn:abc"/>
Although this method creates the new object in the context of the document, it does not automatically add the new object to the document tree. To add the new object, you must explicitly call one of the node insert methods.
According to the W3C Extensible Markup Language (XML) 1.0 recommendation, Element nodes are allowed within Document and Element nodes, and in EntityReference nodes when the EntityReference is outside an Attribute node.
This method is a Microsoft extension to the Document Object Model (DOM).
Product | Versions |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 2.0, 2.1 |
UWP | 10.0 |
.NET feedback
.NET is an open source project. Select a link to provide feedback:
Please sign in to use this experience.
Sign in