Creates a XmlNode with the specified XmlNodeType, Prefix, Name, and NamespaceURI.
Assembly: System.Xml (in System.Xml.dll)
Public Overridable Function CreateNode ( _ type As XmlNodeType, _ prefix As String, _ name As String, _ namespaceURI As String _ ) As XmlNode
public virtual XmlNode CreateNode( XmlNodeType type, string prefix, string name, string namespaceURI )
public: virtual XmlNode^ CreateNode( XmlNodeType type, String^ prefix, String^ name, String^ namespaceURI )
abstract CreateNode : type:XmlNodeType * prefix:string * name:string * namespaceURI:string -> XmlNode override CreateNode : type:XmlNodeType * prefix:string * name:string * namespaceURI:string -> XmlNode
Parameters
- type
- Type: System.Xml.XmlNodeType
The XmlNodeType of the new node.
- prefix
- Type: System.String
The prefix of the new node.
- name
- Type: System.String
The local name of the new node.
- namespaceURI
- Type: System.String
The namespace URI of the new node.
| Exception | Condition |
|---|---|
| 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 the Element node.
This method is a Microsoft extension to the Document Object Model (DOM).
The following example adds a new element to the document.
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 newElem = doc.CreateNode(XmlNodeType.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
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; newElem = doc.CreateNode(XmlNodeType.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); } }
#using <System.Xml.dll> using namespace System; using namespace System::Xml; int main() { XmlDocument^ doc = gcnew XmlDocument; doc->LoadXml( "<book> <title>Oberon's Legacy</title> <price>5.95</price></book>" ); // Create a new element node. XmlNode^ newElem; newElem = doc->CreateNode( XmlNodeType::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 ); }
.NET Framework
Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0.NET Framework Client Profile
Supported in: 4, 3.5 SP1Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.