XmlDocument.CreateElement Metoda

Definicja

Tworzy element XmlElement.

Przeciążenia

CreateElement(String)

Tworzy element o określonej nazwie.

CreateElement(String, String)

Tworzy element XmlElement z kwalifikowaną nazwą i NamespaceURI.

CreateElement(String, String, String)

Tworzy element z określonymi Prefixelementami , LocalNamei NamespaceURI.

CreateElement(String)

Tworzy element o określonej nazwie.

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

Parametry

name
String

Kwalifikowana nazwa elementu. Jeśli nazwa zawiera dwukropek, właściwość odzwierciedla część nazwy poprzedzającej dwukropek Prefix , a LocalName właściwość odzwierciedla część nazwy po dwukropku. Kwalifikowana nazwa nie może zawierać prefiksu "xmlns".

Zwraca

XmlElement

Nowy XmlElementelement .

Przykłady

Poniższy przykład tworzy nowy element i dodaje go do dokumentu.

#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

Uwagi

Należy pamiętać, że zwrócone wystąpienie implementuje XmlElement interfejs, więc atrybuty domyślne zostaną utworzone bezpośrednio na zwróconym obiekcie.

Mimo że ta metoda tworzy nowy obiekt w kontekście dokumentu, nie dodaje automatycznie nowego obiektu do drzewa dokumentów. Aby dodać nowy obiekt, należy jawnie wywołać jedną z metod wstawiania węzła.

Zgodnie z zaleceniem W3C Extensible Markup Language (XML) 1.0 węzły elementu są dozwolone w węzłach Document i Element oraz w węzłach EntityReference, gdy węzeł EntityReference nie jest elementem podrzędnym węzła Atrybut.

Dotyczy

CreateElement(String, String)

Tworzy element XmlElement z kwalifikowaną nazwą i 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

Parametry

qualifiedName
String

Kwalifikowana nazwa elementu. Jeśli nazwa zawiera dwukropek, Prefix właściwość będzie odzwierciedlać część nazwy poprzedzającej dwukropek, a LocalName właściwość będzie odzwierciedlać część nazwy po dwukropku. Kwalifikowana nazwa nie może zawierać prefiksu "xmlns".

namespaceURI
String

Identyfikator URI przestrzeni nazw elementu.

Zwraca

XmlElement

Nowy XmlElementelement .

Uwagi

Poniższy kod w języku C#

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

element, który jest odpowiednikiem następującego tekstu XML.

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

Mimo że ta metoda tworzy nowy obiekt w kontekście dokumentu, nie dodaje automatycznie nowego obiektu do drzewa dokumentów. Aby dodać nowy obiekt, należy jawnie wywołać jedną z metod wstawiania węzła.

Zgodnie z zaleceniem W3C Extensible Markup Language (XML) 1.0 węzły elementu są dozwolone w węzłach Document i Element, a w węzłach EntityReference węzły, gdy węzeł EntityReference nie jest elementem podrzędnym węzła Atrybut.

Dotyczy

CreateElement(String, String, String)

Tworzy element z określonymi Prefixelementami , LocalNamei NamespaceURI.

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

Parametry

prefix
String

Prefiks nowego elementu (jeśli istnieje). String.Empty i null są równoważne.

localName
String

Lokalna nazwa nowego elementu.

namespaceURI
String

Identyfikator URI przestrzeni nazw nowego elementu (jeśli istnieje). String.Empty i null są równoważne.

Zwraca

XmlElement

Nowy XmlElementelement .

Przykłady

Poniższy przykład dodaje nowy element do istniejącego dokumentu 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;
   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

Uwagi

Poniższy kod w języku C#

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

Tworzy element odpowiadający następującemu tekstowi XML:

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

Mimo że ta metoda tworzy nowy obiekt w kontekście dokumentu, nie dodaje automatycznie nowego obiektu do drzewa dokumentów. Aby dodać nowy obiekt, należy jawnie wywołać jedną z metod wstawiania węzła.

Zgodnie z zaleceniem W3C Extensible Markup Language (XML) 1.0 węzły elementu są dozwolone w węzłach Document i Element, a w węzłach EntityReference, gdy element EntityReference znajduje się poza węzłem Atrybut.

Ta metoda jest rozszerzeniem firmy Microsoft do modelu obiektów dokumentów (DOM).

Dotyczy