XmlDocument.CreateElement Metodo

Definizione

Crea un oggetto XmlElement.

Overload

Nome Descrizione
CreateElement(String)

Crea un elemento con il nome specificato.

CreateElement(String, String)

Crea un oggetto XmlElement con il nome completo e NamespaceURI.

CreateElement(String, String, String)

Crea un elemento con l'oggetto , LocalNamee NamespaceURIspecificatoPrefix.

CreateElement(String)

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 due punti, la Prefix proprietà riflette la parte del nome che precede i due punti e la LocalName proprietà riflette la parte del nome dopo i due punti. Il nome completo non può includere un prefisso "xmlns".

Valori restituiti

XmlElementNuovo oggetto .

Esempio

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

using System;
using System.Xml;

public class Sample
{
  public static void CreateTextNodeExample()
  {
    // Create the XmlDocument.
    XmlDocument doc = new();
    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);
  }
}
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

L'esempio produce l'output seguente:

Display the modified XML...
<?xml version="1.0" encoding="utf-8"?>
<book genre="novel" ISBN="1-861001-57-5">
  <title>Pride And Prejudice</title>
  <price>19.95</price>
</book>

Commenti

Si noti che l'istanza restituita implementa l'interfaccia XmlElement , quindi gli attributi predefiniti verranno creati direttamente nell'oggetto 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.

In base alla 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 Attribute.

Si applica a

CreateElement(String, String)

Crea un oggetto XmlElement con il nome completo e NamespaceURI.

public:
 System::Xml::XmlElement ^ CreateElement(System::String ^ qualifiedName, System::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 due punti, la Prefix proprietà rifletterà la parte del nome che precede i due punti e la LocalName proprietà rifletterà la parte del nome dopo i due punti. Il nome completo non può includere un prefisso "xmlns".

namespaceURI
String

URI dello spazio dei nomi dell'elemento.

Valori restituiti

XmlElementNuovo oggetto .

Commenti

Il codice C# seguente:

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

... restituisce un elemento equivalente al codice 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.

In base alla 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 Attribute.

Si applica a

CreateElement(String, String, String)

Crea un elemento con l'oggetto , LocalNamee NamespaceURIspecificatoPrefix.

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

Valori restituiti

XmlElementNuovo oggetto .

Esempio

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

using System;
using System.IO;
using System.Xml;

public class Sample1
{
  public static void CreateElementExample()
  {
    // Create the XmlDocument.
    XmlDocument doc = new();
    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

Il codice C# seguente:

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

... crea un elemento equivalente al codice 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.

In base alla 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 EntityReference si trova all'esterno di un nodo Attribute.

Questo metodo è un'estensione Microsoft al dom (Document Object Model).

Si applica a