XmlDocument.CreateElement Metodo

Definizione

Crea un oggetto XmlElement.

Overload

CreateElement(String)

Crea un elemento con il nome specificato.

CreateElement(String, String)

Crea un oggetto XmlElement con il nome completo e una proprietà NamespaceURI.

CreateElement(String, String, String)

Crea un elemento con le proprietà Prefix, LocalName e NamespaceURI specificate.

CreateElement(String)

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

Crea un elemento con il nome specificato.

public:
 System::Xml::XmlElement ^ CreateElement(System::String ^ name);
public System.Xml.XmlElement CreateElement (string name);
member this.CreateElement : string -> System.Xml.XmlElement
Public Function CreateElement (name As String) As XmlElement

Parametri

name
String

Nome completo dell'elemento. Se il nome contiene i due punti, la proprietà Prefix riflette la parte del nome che precede i due punti e la proprietà LocalName la parte che li segue. Il nome completo non può includere un prefisso"xmlns".

Restituisce

Nuovo oggetto XmlElement.

Esempio

Nell'esempio seguente viene creato un nuovo elemento e lo aggiunge al documento.

#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.
   //The text node is the content of the price element.
   XmlElement^ elem = doc->CreateElement( "price" );
   XmlText^ text = doc->CreateTextNode( "19.95" );
   doc->DocumentElement->AppendChild( elem );
   doc->DocumentElement->LastChild->AppendChild( text );
   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.
    //The text node is the content of the price element.
    XmlElement elem = doc.CreateElement("price");
    XmlText text = doc.CreateTextNode("19.95");
    doc.DocumentElement.AppendChild(elem);
    doc.DocumentElement.LastChild.AppendChild(text);

    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.
        'The text node is the content of the price element.
        Dim elem As XmlElement = doc.CreateElement("price")
        Dim text As XmlText = doc.CreateTextNode("19.95")
        doc.DocumentElement.AppendChild(elem)
        doc.DocumentElement.LastChild.AppendChild(text)
        
        Console.WriteLine("Display the modified XML...")
        doc.Save(Console.Out)
    End Sub
End Class

Commenti

Si noti che l'istanza restituita implementa l'interfaccia, pertanto gli attributi predefiniti verranno creati direttamente nell'oggetto XmlElement restituito.

Anche se questo metodo crea il nuovo oggetto nel contesto del documento, non aggiunge automaticamente il nuovo oggetto all'albero del documento. Per aggiungere il nuovo oggetto, è necessario chiamare in modo esplicito uno dei metodi di inserimento del nodo.

Secondo la raccomandazione W3C Extensible Markup Language (XML) 1.0, i nodi elemento sono consentiti all'interno dei nodi Document ed Element e nei nodi EntityReference quando il nodo EntityReference non è figlio di un nodo Attributo.

Si applica a

CreateElement(String, String)

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

Crea un oggetto XmlElement con il nome completo e una proprietà NamespaceURI.

public:
 System::Xml::XmlElement ^ CreateElement(System::String ^ qualifiedName, System::String ^ namespaceURI);
public System.Xml.XmlElement CreateElement (string qualifiedName, string namespaceURI);
public System.Xml.XmlElement CreateElement (string qualifiedName, string? namespaceURI);
member this.CreateElement : string * string -> System.Xml.XmlElement
Public Function CreateElement (qualifiedName As String, namespaceURI As String) As XmlElement

Parametri

qualifiedName
String

Nome completo dell'elemento. Se il nome contiene i due punti, la proprietà Prefix rifletterà la parte del nome che precede i due punti e la proprietà LocalName la parte che li segue. Il nome completo non può includere un prefisso"xmlns".

namespaceURI
String

URI dello spazio dei nomi dell'elemento.

Restituisce

Nuovo oggetto XmlElement.

Commenti

Codice C# seguente

XmlElement elem;
elem=doc.CreateElement("xy:item", "urn:abc");

genera un elemento equivalente al testo XML seguente.

<xy:item
       xmlns:xy="urn:abc"/>

Anche se questo metodo crea il nuovo oggetto nel contesto del documento, non aggiunge automaticamente il nuovo oggetto all'albero del documento. Per aggiungere il nuovo oggetto, è necessario chiamare in modo esplicito uno dei metodi di inserimento del nodo.

Secondo la raccomandazione W3C Extensible Markup Language (XML) 1.0, i nodi elemento sono consentiti all'interno dei nodi Document ed Element e nei nodi EntityReference quando il nodo EntityReference non è figlio di un nodo Attributo.

Si applica a

CreateElement(String, String, String)

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

Crea un elemento con le proprietà Prefix, LocalName e NamespaceURI specificate.

public:
 virtual System::Xml::XmlElement ^ CreateElement(System::String ^ prefix, System::String ^ localName, System::String ^ namespaceURI);
public virtual System.Xml.XmlElement CreateElement (string prefix, string localName, string namespaceURI);
public virtual System.Xml.XmlElement CreateElement (string? prefix, string localName, string? namespaceURI);
abstract member CreateElement : string * string * string -> System.Xml.XmlElement
override this.CreateElement : string * string * string -> System.Xml.XmlElement
Public Overridable Function CreateElement (prefix As String, localName As String, namespaceURI As String) As XmlElement

Parametri

prefix
String

Prefisso del nuovo elemento, se presente. String.Empty e null sono equivalenti.

localName
String

Nome locale del nuovo elemento.

namespaceURI
String

URI dello spazio dei nomi del nuovo elemento, se presente. String.Empty e null sono equivalenti.

Restituisce

Nuovo oggetto XmlElement.

Esempio

Nell'esempio seguente viene aggiunto un nuovo elemento al documento XML esistente.

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   
   // Create the XmlDocument.
   XmlDocument^ doc = gcnew XmlDocument;
   String^ xmlData = "<book xmlns:bk='urn:samples'></book>";
   doc->Load( gcnew StringReader( xmlData ) );
   
   // Create a new element and add it to the document.
   XmlElement^ elem = doc->CreateElement( "bk", "genre", "urn:samples" );
   elem->InnerText = "fantasy";
   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();
    string xmlData = "<book xmlns:bk='urn:samples'></book>";

    doc.Load(new StringReader(xmlData));

    // Create a new element and add it to the document.
    XmlElement elem = doc.CreateElement("bk", "genre", "urn:samples");
    elem.InnerText = "fantasy";
    doc.DocumentElement.AppendChild(elem);

    Console.WriteLine("Display the modified XML...");
    doc.Save(Console.Out);
  }
}
Imports System.IO
Imports System.Xml

public class Sample 

  public shared sub Main() 

    ' Create the XmlDocument.
    Dim doc as XmlDocument = new XmlDocument()
    Dim xmlData as string = "<book xmlns:bk='urn:samples'></book>"

    doc.Load(new StringReader(xmlData))

    ' Create a new element and add it to the document.
    Dim elem as XmlElement = doc.CreateElement("bk", "genre", "urn:samples")
    elem.InnerText = "fantasy"
    doc.DocumentElement.AppendChild(elem)

    Console.WriteLine("Display the modified XML...")
    doc.Save(Console.Out)

  end sub
end class

Commenti

Codice C# seguente

XmlElement elem;
elem=doc.CreateElement("xy", "item", "urn:abc");

crea un elemento equivalente al testo XML seguente:

<xy:item xmlns:xy="urn:abc"/>

Anche se questo metodo crea il nuovo oggetto nel contesto del documento, non aggiunge automaticamente il nuovo oggetto all'albero del documento. Per aggiungere il nuovo oggetto, è necessario chiamare in modo esplicito uno dei metodi di inserimento del nodo.

Secondo la raccomandazione W3C Extensible Markup Language (XML) 1.0, i nodi elemento sono consentiti all'interno dei nodi Document e Element e nei nodi EntityReference quando EntityReference non è esterno a un nodo Attributo.

Questo metodo è un'estensione Microsoft al modello a oggetti document (DOM).

Si applica a