.NET Framework Class Library
XmlDocument.CreateNode Method (String, String, String)

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

[Visual Basic]
Overloads Public Overridable Function CreateNode( _
   ByVal nodeTypeString As String, _
   ByVal name As String, _
   ByVal namespaceURI As String _
) As XmlNode
[C#]
public virtual XmlNode CreateNode(
 string nodeTypeString,
 string name,
 string namespaceURI
);
[C++]
public: virtual XmlNode* CreateNode(
 String* nodeTypeString,
 String* name,
 String* namespaceURI
);
[JScript]
public function CreateNode(
   nodeTypeString : String,
 name : String,
 namespaceURI : String
) : XmlNode;

Parameters

nodeTypeString
String version of the XmlNodeType of the new node. This parameter must be one of the values listed in the table below.
name
The qualified name of the new node. If the name contains a colon, it is parsed into Prefix and LocalName components.
namespaceURI
The namespace URI of the new node.

Return Value

The new XmlNode.

Exceptions

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

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 (www.w3.org/TR/1998/REC-xml-19980210).

  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).

Example

[Visual Basic, C#, C++] The following example creates a new element and inserts it into the document.

[Visual Basic] 
Imports System
Imports System.Xml
 
public class Sample 

  public shared sub Main() 

       Dim doc as XmlDocument = new XmlDocument()
       doc.LoadXml("<book>" & _
                   "  <title>Oberon's Legacy</title>" & _
                   "  <price>5.95</price>" & _
                   "</book>") 
 
       ' Create a new element node.
       Dim newElem as XmlNode = doc.CreateNode("element", "pages", "")  
       newElem.InnerText = "290"
     
       Console.WriteLine("Add the new element to the document...")
       Dim root as XmlElement = doc.DocumentElement
       root.AppendChild(newElem)
     
       Console.WriteLine("Display the modified XML document...")
       Console.WriteLine(doc.OuterXml)
   end sub
end class

[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);
   }
 }

[C++] 
#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::Xml;
 
int main()
{
       XmlDocument* doc = new XmlDocument();
       doc->LoadXml(S"<book>  <title>Oberon's Legacy</title>  <price>5.95</price></book>"); 
 
       // Create a new element node.
       XmlNode* newElem = doc->CreateNode(S"element", S"pages", S"");  
       newElem->InnerText = S"290";
     
       Console::WriteLine(S"Add the new element to the document...");
       XmlElement* root = doc->DocumentElement;
       root->AppendChild(newElem);
     
       Console::WriteLine(S"Display the modified XML document...");
       Console::WriteLine(doc->OuterXml);
}

[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.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

XmlDocument Class | XmlDocument Members | System.Xml Namespace | XmlDocument.CreateNode Overload List

Page view tracker