XmlDocument.CreateNode Method (XmlNodeType, String, String)

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

Namespace:  System.Xml
Assembly:  System.Xml (in System.Xml.dll)

'Declaration
Public Overridable Function CreateNode ( _
	type As XmlNodeType, _
	name As String, _
	namespaceURI As String _
) As XmlNode

Parameters

type
Type: System.Xml.XmlNodeType

The XmlNodeType of the new node.

name
Type: System.String

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

namespaceURI
Type: System.String

The namespace URI of the new node.

Return Value

Type: System.Xml.XmlNode
The new XmlNode.

ExceptionCondition
ArgumentException

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

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

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

Option Explicit
Option Strict

Imports System
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. 
        Dim elem As XmlNode = doc.CreateNode(XmlNodeType.Element, "price", Nothing)
        elem.InnerText = "19.95"
        doc.DocumentElement.AppendChild(elem)

        Console.WriteLine("Display the modified XML...")
        doc.Save(Console.Out)
    End Sub 'Main 
End Class 'Sample

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft