XmlDocument.CreateNode Methode

Definition

Erstellt ein XmlNode.

Überlädt

Name Beschreibung
CreateNode(String, String, String)

Erstellt einen XmlNode mit dem angegebenen Knotentyp, Nameund NamespaceURI.

CreateNode(XmlNodeType, String, String)

Erstellt eine XmlNode mit dem angegebenen XmlNodeType, Name, und NamespaceURI.

CreateNode(XmlNodeType, String, String, String)

Erstellt eine XmlNode mit dem angegebenen XmlNodeType, , Prefix, Name, und NamespaceURI.

CreateNode(String, String, String)

Quelle:
XmlDocument.cs
Quelle:
XmlDocument.cs
Quelle:
XmlDocument.cs
Quelle:
XmlDocument.cs
Quelle:
XmlDocument.cs

Erstellt einen XmlNode mit dem angegebenen Knotentyp, Nameund NamespaceURI.

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

Parameter

nodeTypeString
String

Zeichenfolgenversion des XmlNodeType neuen Knotens. Dieser Parameter muss einer der werte sein, die in der folgenden Tabelle aufgeführt sind.

name
String

Der qualifizierte Name des neuen Knotens. Wenn der Name einen Doppelpunkt enthält, wird er in Prefix und LocalName Komponenten analysiert.

namespaceURI
String

Der Namespace-URI des neuen Knotens.

Gibt zurück

Die neue XmlNode.

Ausnahmen

Der Name wurde nicht angegeben und XmlNodeType erfordert einen Namen; oder nodeTypeString ist keine der unten aufgeführten Zeichenfolgen.

Beispiele

Im folgenden Beispiel wird ein neues Element erstellt und in das Dokument eingefügt.

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

Hinweise

Bei dem nodeTypeString Parameter wird die Groß-/Kleinschreibung beachtet, und es muss sich um einen der Werte in der folgenden Tabelle handeln.

nodeTypeString Xmlnodetype
Attribut Attribute
cdatasection CDATA
Kommentar Comment
Dokument Dokument
documentfragment DocumentFragment
Dokumenttyp Dokumenttyp
Element Element
Entityreference EntityReference
Processinginstruction Verarbeitungsanweisung
signifikanteWhitespace Signifikantes Leerzeichen
Text Text
whitespace Leerzeichen

Obwohl diese Methode das neue Objekt im Kontext des Dokuments erstellt, wird das neue Objekt nicht automatisch der Dokumentstruktur hinzugefügt. Um das neue Objekt hinzuzufügen, müssen Sie explizit eine der Methoden zum Einfügen von Knoten aufrufen.

In der folgenden Tabelle wird gezeigt, welche NodeType[Zeile] innerhalb eines anderen NodeType[Column] gemäß der Empfehlung W3C Extensible Markup Language (XML) 1.0 zulässig ist.

Knotentyp Dokument Dokumenttyp XML-Deklaration Element Attribute Text CDATA Markup EntityReference
Document Nein Nein Nein Nein Nein Nein Nein Nein Nein
DocumentType Ja Nein Nein Nein Nein Nein Nein Nein Nein
XmlDeclaration Ja* Nein Nein Nein Nein Nein Nein Nein Nein
Element Ja Nein Nein Ja Nein Nein Nein Nein Ja†
Attribute Nein Nein Nein Ja Nein Nein Nein Nein Nein
Text Nein Nein Nein Ja Ja Nein Nein Nein Ja
CDATA Nein Nein Nein Ja Nein Nein Nein Nein Ja†
Markup § Ja Nein Nein Ja Nein Nein Nein Nein Nein
EntityReference Nein Nein Nein Ja Ja Nein Nein Nein Ja

* Der XmlDeclaration Knoten muss das erste untergeordnete Element des Dokumentknotens sein.

§ Markup umfasst ProcessingInstruction und Comment Knoten.

Element† und CDATA Knoten sind nur in EntityReference Knoten zulässig, wenn der EntityReference Knoten kein untergeordnetes Element eines Attribute Knotens ist.

** Attribute sind keine untergeordneten Elemente eines Element Knotens. Attribute sind in einer Attributauflistung enthalten, die zu einem Element Knoten gehört.

Diese Methode ist eine Microsoft Erweiterung für das Document Object Model (DOM).

Gilt für:

CreateNode(XmlNodeType, String, String)

Quelle:
XmlDocument.cs
Quelle:
XmlDocument.cs
Quelle:
XmlDocument.cs
Quelle:
XmlDocument.cs
Quelle:
XmlDocument.cs

Erstellt eine XmlNode mit dem angegebenen XmlNodeType, Name, und NamespaceURI.

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

Parameter

type
XmlNodeType

Der XmlNodeType des neuen Knotens.

name
String

Der qualifizierte Name des neuen Knotens. Wenn der Name einen Doppelpunkt enthält, wird er in Prefix und LocalName Komponenten analysiert.

namespaceURI
String

Der Namespace-URI des neuen Knotens.

Gibt zurück

Die neue XmlNode.

Ausnahmen

Der Name wurde nicht angegeben und XmlNodeType erfordert einen Namen.

Beispiele

Im folgenden Beispiel wird ein neues Element erstellt und in ein XML-Dokument eingefügt.

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

Hinweise

Obwohl diese Methode das neue Objekt im Kontext des Dokuments erstellt, wird das neue Objekt nicht automatisch der Dokumentstruktur hinzugefügt. Um das neue Objekt hinzuzufügen, müssen Sie explizit eine der Methoden zum Einfügen von Knoten aufrufen.

In der folgenden Tabelle wird gezeigt, welche NodeType[Zeile] innerhalb eines anderen NodeType[Column] gemäß der Empfehlung W3C Extensible Markup Language (XML) 1.0 zulässig ist.

Knotentyp Dokument Dokumenttyp XML-Deklaration Element Attribute Text CDATA Markup EntityReference
Document Nein Nein Nein Nein Nein Nein Nein Nein Nein
DocumentType Ja Nein Nein Nein Nein Nein Nein Nein Nein
XmlDeclaration Ja* Nein Nein Nein Nein Nein Nein Nein Nein
Element Ja Nein Nein Ja Nein Nein Nein Nein Ja†
Attribute Nein Nein Nein Ja– Nein Nein Nein Nein Nein
Text Nein Nein Nein Ja Ja Nein Nein Nein Ja
CDATA Nein Nein Nein Ja Nein Nein Nein Nein Ja†
Markup§ Ja Nein Nein Ja Nein Nein Nein Nein Nein
EntityReference Nein Nein Nein Ja Ja Nein Nein Nein Ja

* Der XmlDeclaration Knoten muss das erste untergeordnete Element des Dokumentknotens sein.

§ Markup umfasst ProcessingInstruction und Comment Knoten.

Element† und CDATA Knoten sind nur in EntityReference Knoten zulässig, wenn der EntityReference Knoten kein untergeordnetes Element eines Attribute Knotens ist.

** Attribute sind keine untergeordneten Elemente eines Element Knotens. Attribute sind in einer Attributauflistung enthalten, die zu einem Element Knoten gehört.

Diese Methode ist eine Microsoft Erweiterung für das Document Object Model (DOM).

Gilt für:

CreateNode(XmlNodeType, String, String, String)

Quelle:
XmlDocument.cs
Quelle:
XmlDocument.cs
Quelle:
XmlDocument.cs
Quelle:
XmlDocument.cs
Quelle:
XmlDocument.cs

Erstellt eine XmlNode mit dem angegebenen XmlNodeType, , Prefix, Name, und NamespaceURI.

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

Parameter

type
XmlNodeType

Der XmlNodeType des neuen Knotens.

prefix
String

Das Präfix des neuen Knotens.

name
String

Der lokale Name des neuen Knotens.

namespaceURI
String

Der Namespace-URI des neuen Knotens.

Gibt zurück

Die neue XmlNode.

Ausnahmen

Der Name wurde nicht angegeben und XmlNodeType erfordert einen Namen.

Beispiele

Im folgenden Beispiel wird dem Dokument ein neues Element hinzugefügt.

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

Hinweise

Obwohl diese Methode das neue Objekt im Kontext des Dokuments erstellt, wird das neue Objekt nicht automatisch der Dokumentstruktur hinzugefügt. Um das neue Objekt hinzuzufügen, müssen Sie explizit eine der Methoden zum Einfügen von Knoten aufrufen.

In der folgenden Tabelle wird gezeigt, welche NodeType[Zeile] innerhalb eines anderen NodeType[Column] gemäß der Empfehlung W3C Extensible Markup Language (XML) 1.0 zulässig ist.

Dokument Dokumenttyp XML-Deklaration Element Attribute Text CDATA Markup EntityReference
Document Nein Nein Nein Nein Nein Nein Nein Nein Nein
DocumentType Ja Nein Nein Nein Nein Nein Nein Nein Nein
XmlDeclaration Ja* Nein Nein Nein Nein Nein Nein Nein Nein
Element Ja Nein Nein Ja Nein Nein Nein Nein Ja†
Attribute Nein Nein Nein Ja– Nein Nein Nein Nein Nein
Text Nein Nein Nein Ja Ja Nein Nein Nein Ja
CDATA Nein Nein Nein Ja Nein Nein Nein Nein Ja†
Markup§ Ja Nein Nein Ja Nein Nein Nein Nein Nein
EntityReference Nein Nein Nein Ja Ja Nein Nein Nein Ja

* Der XmlDeclaration-Knoten muss das erste untergeordnete Element des Dokumentknotens sein.

§ Markup umfasst ProcessingInstruction und Comment Knoten.

Element† und CDATA Knoten sind nur in EntityReference Knoten zulässig, wenn der EntityReference Knoten kein untergeordnetes Element eines Attribute Knotens ist.

** Attribute sind keine untergeordneten Elemente eines Element Knotens. Attribute sind in einer Attributauflistung enthalten, die zu einem Element Knoten gehört.

Diese Methode ist eine Microsoft Erweiterung für das Document Object Model (DOM).

Gilt für: