XmlDocument.CreateNode Method

Definition

Creates an XmlNode.

Overloads

CreateNode(String, String, String)

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

Creates an XmlNode with the specified node type, Name, and NamespaceURI.

C#
public virtual System.Xml.XmlNode CreateNode(string nodeTypeString, string name, string namespaceURI);
C#
public virtual System.Xml.XmlNode CreateNode(string nodeTypeString, string name, string? namespaceURI);

Parameters

nodeTypeString
String

String version of the XmlNodeType of the new node. This parameter must be one of the values listed in the table below.

name
String

The qualified name of the new node. If the name contains a colon, it is parsed into Prefix and LocalName components.

namespaceURI
String

The namespace URI of the new node.

Returns

The new XmlNode.

Exceptions

The name was not provided and the XmlNodeType requires a name; or nodeTypeString is not one of the strings listed below.

Examples

The following example creates a new element and inserts it into the document.

C#
using System;
using System.Xml;

public class Sample {

  public static void Main() {

       XmlDocument doc = new XmlDocument();
       doc.LoadXml("<book>" +
                   "  <title>Oberon's Legacy</title>" +
                   "  <price>5.95</price>" +
                   "</book>");

       // Create a new element node.
       XmlNode newElem = doc.CreateNode("element", "pages", "");
       newElem.InnerText = "290";

       Console.WriteLine("Add the new element to the document...");
       XmlElement root = doc.DocumentElement;
       root.AppendChild(newElem);

       Console.WriteLine("Display the modified XML document...");
       Console.WriteLine(doc.OuterXml);
   }
 }

Remarks

The nodeTypeString parameter is case sensitive and must be one of the values in the following table.

nodeTypeString XmlNodeType
attribute Attribute
cdatasection CDATA
comment Comment
document Document
documentfragment DocumentFragment
documenttype DocumentType
element Element
entityreference EntityReference
processinginstruction ProcessingInstruction
significantwhitespace SignificantWhitespace
text Text
whitespace Whitespace

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.

The following table shows you what NodeType[row] is allowed inside another NodeType[column] according to the W3C Extensible Markup Language (XML) 1.0 recommendation.

Node type Document DocumentType XmlDeclaration Element Attribute Text CDATA Markup EntityReference
Document no no no no no no no no no
DocumentType yes no no no no no no no no
XmlDeclaration yes* no no no no no no no no
Element yes no no yes no no no no yes†
Attribute no no no yes no no no no no
Text no no no yes yes no no no yes
CDATA no no no yes no no no no yes†
Markup§ yes no no yes no no no no no
EntityReference no no no yes yes no no no yes

* The XmlDeclaration node must be the first child of the Document node.

§ Markup includes ProcessingInstruction and Comment nodes.

Element and CDATA nodes are only allowed in EntityReference nodes when the EntityReference node is not a child of an Attribute node.

‡ Attributes are not children of an Element node. Attributes are contained inside an attribute collection that belongs to an Element node.

This method is a Microsoft extension to the Document Object Model (DOM).

Applies to

.NET 10 and other versions
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

CreateNode(XmlNodeType, String, String)

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

Creates an XmlNode with the specified XmlNodeType, Name, and NamespaceURI.

C#
public virtual System.Xml.XmlNode CreateNode(System.Xml.XmlNodeType type, string name, string namespaceURI);
C#
public virtual System.Xml.XmlNode CreateNode(System.Xml.XmlNodeType type, string name, string? namespaceURI);

Parameters

type
XmlNodeType

The XmlNodeType of the new node.

name
String

The qualified name of the new node. If the name contains a colon then it is parsed into Prefix and LocalName components.

namespaceURI
String

The namespace URI of the new node.

Returns

The new XmlNode.

Exceptions

The name was not provided and the XmlNodeType requires a name.

Examples

The following example creates a new element and inserts it into an XML document.

C#
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.
    XmlNode elem = doc.CreateNode(XmlNodeType.Element, "price", null);
    elem.InnerText = "19.95";
    doc.DocumentElement.AppendChild(elem);

    Console.WriteLine("Display the modified XML...");
    doc.Save(Console.Out);
  }
}

Remarks

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.

The following table shows you what NodeType[row] is allowed inside another NodeType[column] according to the W3C Extensible Markup Language (XML) 1.0 recommendation.

Node type Document DocumentType XmlDeclaration Element Attribute Text CDATA Markup EntityReference
Document no no no no no no no no no
DocumentType yes no no no no no no no no
XmlDeclaration yes* no no no no no no no no
Element yes no no yes no no no no yes†
Attribute no no no yes‡ no no no no no
Text no no no yes yes no no no yes
CDATA no no no yes no no no no yes†
Markup§ yes no no yes no no no no no
EntityReference no no no yes yes no no no yes

* The XmlDeclaration node must be the first child of the Document node.

§ Markup includes ProcessingInstruction and Comment nodes.

Element and CDATA nodes are only allowed in EntityReference nodes when the EntityReference node is not a child of an Attribute node.

‡ Attributes are not children of an Element node. Attributes are contained inside an attribute collection that belongs to an Element node.

This method is a Microsoft extension to the Document Object Model (DOM).

Applies to

.NET 10 and other versions
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

CreateNode(XmlNodeType, String, String, String)

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

Creates a XmlNode with the specified XmlNodeType, Prefix, Name, and NamespaceURI.

C#
public virtual System.Xml.XmlNode CreateNode(System.Xml.XmlNodeType type, string prefix, string name, string namespaceURI);
C#
public virtual System.Xml.XmlNode CreateNode(System.Xml.XmlNodeType type, string? prefix, string name, string? namespaceURI);

Parameters

type
XmlNodeType

The XmlNodeType of the new node.

prefix
String

The prefix of the new node.

name
String

The local name of the new node.

namespaceURI
String

The namespace URI of the new node.

Returns

The new XmlNode.

Exceptions

The name was not provided and the XmlNodeType requires a name.

Examples

The following example adds a new element to the document.

C#
using System;
using System.Xml;

public class Sample {

  public static void Main() {

        // Create a new document containing information about a book
        XmlDocument doc = new XmlDocument();
        doc.LoadXml("<book>" +
                    "  <title>Oberon's Legacy</title>" +
                    "  <price>5.95</price>" +
                    "</book>");

        // Create a new element node for the ISBN of the book
        // It is possible to supply a prefix for this node, and specify a qualified namespace.
        XmlNode newElem;
        newElem = doc.CreateNode(XmlNodeType.Element, "g", "ISBN", "https://global.ISBN/list");
        newElem.InnerText = "1-861001-57-5";

        // Add the new element to the document
        XmlElement root = doc.DocumentElement;
        root.AppendChild(newElem);

        // Display the modified XML document
        Console.WriteLine(doc.OuterXml);

        //Output:
        // <book><title>Oberon's Legacy</title><price>5.95</price><g:ISBN xmlns:g="https://global.ISBN/list">1-861001-57-5</g:ISBN></book>
   }
 }

Remarks

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.

The following table shows you what NodeType[row] is allowed inside another NodeType[column] according to the W3C Extensible Markup Language (XML) 1.0 recommendation.

Document DocumentType XmlDeclaration Element Attribute Text CDATA Markup EntityReference
Document no no no no no no no no no
DocumentType yes no no no no no no no no
XmlDeclaration yes* no no no no no no no no
Element yes no no yes no no no no yes†
Attribute no no no yes‡ no no no no no
Text no no no yes yes no no no yes
CDATA no no no yes no no no no yes†
Markup§ yes no no yes no no no no no
EntityReference no no no yes yes no no no yes

* The XmlDeclaration node must be the first child of the Document node.

§ Markup includes ProcessingInstruction and Comment nodes.

Element and CDATA nodes are only allowed in EntityReference nodes when the EntityReference node is not a child of an Attribute node.

‡ Attributes are not children of an Element node. Attributes are contained inside an attribute collection that belongs to an Element node.

This method is a Microsoft extension to the Document Object Model (DOM).

Applies to

.NET 10 and other versions
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