XmlDocument.CreateNode Méthode

Définition

Crée un objet XmlNode.

Surcharges

CreateNode(String, String, String)

Crée un élément XmlNode avec le type de nœud, l'élément Name et l'élément NamespaceURI spécifiés.

CreateNode(XmlNodeType, String, String)

Crée un élément XmlNode avec les éléments XmlNodeType, Name et NamespaceURI spécifiés.

CreateNode(XmlNodeType, String, String, String)

Crée un élément XmlNode avec les éléments XmlNodeType, Prefix, Name et NamespaceURI spécifiés.

CreateNode(String, String, String)

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

Crée un élément XmlNode avec le type de nœud, l'élément Name et l'élément NamespaceURI spécifiés.

public:
 virtual System::Xml::XmlNode ^ CreateNode(System::String ^ nodeTypeString, System::String ^ name, System::String ^ namespaceURI);
public virtual System.Xml.XmlNode CreateNode (string nodeTypeString, string name, string namespaceURI);
public virtual System.Xml.XmlNode CreateNode (string nodeTypeString, string name, string? namespaceURI);
abstract member CreateNode : string * string * string -> System.Xml.XmlNode
override this.CreateNode : string * string * string -> System.Xml.XmlNode
Public Overridable Function CreateNode (nodeTypeString As String, name As String, namespaceURI As String) As XmlNode

Paramètres

nodeTypeString
String

Version au format chaîne de l'élément XmlNodeType du nouveau nœud. Ce paramètre doit prendre l'une des valeurs répertoriées dans le tableau suivant.

name
String

Nom qualifié du nouveau nœud. Si le nom contient un signe deux-points, il est analysé en deux composants : Prefix et LocalName.

namespaceURI
String

L'URI de l'espace de noms du nouveau nœud.

Retours

Nouvelle XmlNode.

Exceptions

Le nom n’a pas été fourni alors que XmlNodeType requiert un nom, ou nodeTypeString ne figure pas parmi les chaînes répertoriées ci-dessous.

Exemples

L’exemple suivant crée un élément et l’insère dans le document.

#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 = 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 );
}

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

Remarques

Le nodeTypeString paramètre respecte la casse et doit être l’une des valeurs du tableau suivant.

nodeTypeString XmlNodeType
attribut Attribut
cdatasection CDATA
comment Commentaire
document Document
documentfragment DocumentFragment
type de document DocumentType ;
element Élément
Entityreference EntityReference
processinginstruction ProcessingInstruction ;
significativewhitespace SignificantWhitespace
texte Texte
whitespace Espace blanc

Bien que cette méthode crée le nouvel objet dans le contexte du document, elle n’ajoute pas automatiquement le nouvel objet à l’arborescence du document. Pour ajouter le nouvel objet, vous devez appeler explicitement l’une des méthodes d’insertion de nœud.

Le tableau suivant vous montre ce que NodeType[row] est autorisé à l’intérieur d’une autre NodeType[colonne] conformément à la recommandation XML (Extensible Markup Language) W3C 1.0.

Document DocumentType ; XmlDeclaration Élément Attribut Texte CDATA balisage EntityReference
Document non non non non non non non non non
DocumentType Oui non non non non non non non non
XmlDeclaration oui* non non non non non non non non
Element Oui non non Oui non non non non Oui***
Attribute non non non Oui**** non non non non non
Text non non non Oui Oui non non non Oui
CDATA non non non Oui non non non non Oui***
Markup** Oui non non Oui non non non non non
EntityReference non non non Oui Oui non non non Oui

* Le nœud XmlDeclaration doit être le premier enfant du nœud Document.

** Le balisage inclut les nœuds ProcessingInstruction et Comment.

Les nœuds Element et CDATA sont uniquement autorisés dans les nœuds EntityReference lorsque le nœud EntityReference n’est pas un enfant d’un nœud Attribute.

Les attributs ne sont pas des enfants d’un nœud Element. Les attributs sont contenus dans une collection d’attributs qui appartient à un nœud Element.

Cette méthode est une extension Microsoft du modèle DOM (Document Object Model).

S’applique à

CreateNode(XmlNodeType, String, String)

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

Crée un élément XmlNode avec les éléments XmlNodeType, Name et NamespaceURI spécifiés.

public:
 virtual System::Xml::XmlNode ^ CreateNode(System::Xml::XmlNodeType type, System::String ^ name, System::String ^ namespaceURI);
public virtual System.Xml.XmlNode CreateNode (System.Xml.XmlNodeType type, string name, string namespaceURI);
public virtual System.Xml.XmlNode CreateNode (System.Xml.XmlNodeType type, string name, string? namespaceURI);
abstract member CreateNode : System.Xml.XmlNodeType * string * string -> System.Xml.XmlNode
override this.CreateNode : System.Xml.XmlNodeType * string * string -> System.Xml.XmlNode
Public Overridable Function CreateNode (type As XmlNodeType, name As String, namespaceURI As String) As XmlNode

Paramètres

type
XmlNodeType

XmlNodeType du nouveau nœud.

name
String

Nom qualifié du nouveau nœud. Si le nom contient un signe deux-points, il est analysé en deux composants : Prefix et LocalName.

namespaceURI
String

L'URI de l'espace de noms du nouveau nœud.

Retours

Nouvelle XmlNode.

Exceptions

Le nom n’a pas été fourni alors que XmlNodeType requiert un nom.

Exemples

L’exemple suivant crée un élément et l’insère dans un document XML.

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   
   //Create the XmlDocument.
   XmlDocument^ doc = gcnew 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", nullptr );
   elem->InnerText = "19.95";
   doc->DocumentElement->AppendChild( elem );
   Console::WriteLine( "Display the modified XML..." );
   doc->Save( Console::Out );
}

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);
  }
}
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.
        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
End Class

Remarques

Bien que cette méthode crée le nouvel objet dans le contexte du document, elle n’ajoute pas automatiquement le nouvel objet à l’arborescence du document. Pour ajouter le nouvel objet, vous devez appeler explicitement l’une des méthodes d’insertion de nœud.

Le tableau suivant vous montre ce que NodeType[row] est autorisé à l’intérieur d’une autre NodeType[colonne] conformément à la recommandation XML (Extensible Markup Language) W3C 1.0.

Document DocumentType ; XmlDeclaration Élément Attribut Texte CDATA balisage EntityReference
Document non non non non non non non non non
DocumentType Oui non non non non non non non non
XmlDeclaration oui* non non non non non non non non
Element Oui non non Oui non non non non Oui***
Attribute non non non Oui**** non non non non non
Text non non non Oui Oui non non non Oui
CDATA non non non Oui non non non non Oui***
Markup** Oui non non Oui non non non non non
EntityReference non non non Oui Oui non non non Oui

* Le nœud XmlDeclaration doit être le premier enfant du nœud Document.

** Le balisage inclut les nœuds ProcessingInstruction et Comment.

Les nœuds Element et CDATA ne sont autorisés dans les nœuds EntityReference que lorsque le nœud EntityReference n’est pas un enfant d’un nœud Attribute.

Les attributs ne sont pas des enfants d’un nœud Element. Les attributs sont contenus dans une collection d’attributs qui appartient à un nœud Element.

Cette méthode est une extension Microsoft du dom (Document Object Model).

S’applique à

CreateNode(XmlNodeType, String, String, String)

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

Crée un élément XmlNode avec les éléments XmlNodeType, Prefix, Name et NamespaceURI spécifiés.

public:
 virtual System::Xml::XmlNode ^ CreateNode(System::Xml::XmlNodeType type, System::String ^ prefix, System::String ^ name, System::String ^ namespaceURI);
public virtual System.Xml.XmlNode CreateNode (System.Xml.XmlNodeType type, string prefix, string name, string namespaceURI);
public virtual System.Xml.XmlNode CreateNode (System.Xml.XmlNodeType type, string? prefix, string name, string? namespaceURI);
abstract member CreateNode : System.Xml.XmlNodeType * string * string * string -> System.Xml.XmlNode
override this.CreateNode : System.Xml.XmlNodeType * string * string * string -> System.Xml.XmlNode
Public Overridable Function CreateNode (type As XmlNodeType, prefix As String, name As String, namespaceURI As String) As XmlNode

Paramètres

type
XmlNodeType

XmlNodeType du nouveau nœud.

prefix
String

Préfixe du nouveau nœud.

name
String

Nom local du nouveau nœud.

namespaceURI
String

L'URI de l'espace de noms du nouveau nœud.

Retours

Nouvelle XmlNode.

Exceptions

Le nom n’a pas été fourni alors que XmlNodeType requiert un nom.

Exemples

L’exemple suivant ajoute un nouvel élément au document.

#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, "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>
}

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>
   }
 }
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.
        ' It is possible to supply a prefix for this node, and specify a qualified namespace
        Dim newElem as XmlNode
        newElem = doc.CreateNode(XmlNodeType.Element,"g", "ISBN","https://global.ISBN/list")
        newElem.InnerText = "1-861001-57-5"
     
        ' Add the new element to the document
        Dim root as XmlElement = 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>
   end sub
end class

Remarques

Bien que cette méthode crée le nouvel objet dans le contexte du document, elle n’ajoute pas automatiquement le nouvel objet à l’arborescence du document. Pour ajouter le nouvel objet, vous devez appeler explicitement l’une des méthodes d’insertion de nœud.

Le tableau suivant indique ce que NodeType[row] est autorisé à l’intérieur d’un autre NodeType[colonne] conformément à la recommandation W3C Extensible Markup Language (XML) 1.0.

Document DocumentType ; XmlDeclaration Élément Attribut Texte CDATA balisage EntityReference
Document non non non non non non non non non
DocumentType Oui non non non non non non non non
XmlDeclaration oui* non non non non non non non non
Element Oui non non Oui non non non non Oui***
Attribute non non non Oui**** non non non non non
Text non non non Oui Oui non non non Oui
CDATA non non non Oui non non non non Oui***
Markup** Oui non non Oui non non non non non
EntityReference non non non Oui Oui non non non Oui

* Le nœud XmlDeclaration doit être le premier enfant du nœud Document.

** Le balisage inclut les nœuds ProcessingInstruction et Comment.

Les nœuds Element et CDATA ne sont autorisés dans les nœuds EntityReference que lorsque le nœud EntityReference n’est pas un enfant d’un nœud Attribute.

Les attributs ne sont pas des enfants d’un nœud Element. Les attributs sont contenus dans une collection d’attributs qui appartient au nœud Element.

Cette méthode est une extension Microsoft du dom (Document Object Model).

S’applique à