XmlDocument.CreateElement Yöntem

Tanım

oluşturur XmlElement.

Aşırı Yüklemeler

CreateElement(String)

Belirtilen ada sahip bir öğe oluşturur.

CreateElement(String, String)

ve NamespaceURInitelenmiş adıyla bir XmlElement oluşturur.

CreateElement(String, String, String)

Belirtilen Prefix, LocalNameve NamespaceURIile bir öğe oluşturur.

CreateElement(String)

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

Belirtilen ada sahip bir öğe oluşturur.

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

Parametreler

name
String

Öğesinin tam adı. Ad iki nokta üst üste Prefix içeriyorsa, özellik adın iki nokta üst üsteden önceki bölümünü ve LocalName özellik de adın iki nokta üst üsteden sonraki bölümünü yansıtır. Nitelenmiş ad 'xmlns' ön ekini içeremez.

Döndürülenler

Yeni XmlElement.

Örnekler

Aşağıdaki örnek yeni bir öğe oluşturur ve bunu belgeye ekler.

#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

Açıklamalar

Döndürülen örneğin arabirimini uyguladığını XmlElement , dolayısıyla varsayılan özniteliklerin doğrudan döndürülen nesnede oluşturulacağını unutmayın.

Bu yöntem yeni nesneyi belge bağlamında oluştursa da, yeni nesneyi belge ağacına otomatik olarak eklemez. Yeni nesneyi eklemek için düğüm ekleme yöntemlerinden birini açıkça çağırmanız gerekir.

W3C Genişletilebilir Biçimlendirme Dili (XML) 1.0 önerisine göre, EntityReference düğümü bir Öznitelik düğümünün alt öğesi olmadığında, Öğe düğümlerine Belge ve Öğe düğümleri içinde ve EntityReference düğümlerinde izin verilir.

Şunlara uygulanır

CreateElement(String, String)

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

ve NamespaceURInitelenmiş adıyla bir XmlElement oluşturur.

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

Parametreler

qualifiedName
String

Öğesinin tam adı. Ad iki nokta üst üste Prefix içeriyorsa, özellik adın iki nokta üst üsteden önceki bölümünü yansıtır ve LocalName özellik de adın iki nokta üst üsteden sonraki bölümünü yansıtır. Nitelenmiş ad 'xmlns' ön ekini içeremez.

namespaceURI
String

öğesinin ad alanı URI'si.

Döndürülenler

Yeni XmlElement.

Açıklamalar

Aşağıdaki C# kodu

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

aşağıdaki XML metnine eşdeğer bir öğeyle sonuçlanır.

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

Bu yöntem yeni nesneyi belge bağlamında oluştursa da, yeni nesneyi belge ağacına otomatik olarak eklemez. Yeni nesneyi eklemek için düğüm ekleme yöntemlerinden birini açıkça çağırmanız gerekir.

W3C Genişletilebilir Biçimlendirme Dili (XML) 1.0 önerisine göre, EntityReference düğümü bir Öznitelik düğümünün alt öğesi olmadığında, Öğe düğümlerine Belge ve Öğe düğümleri içinde ve EntityReference düğümlerinde izin verilir.

Şunlara uygulanır

CreateElement(String, String, String)

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

Belirtilen Prefix, LocalNameve NamespaceURIile bir öğe oluşturur.

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

Parametreler

prefix
String

Yeni öğenin ön eki (varsa). String.Empty ve null eşdeğerdir.

localName
String

Yeni öğenin yerel adı.

namespaceURI
String

Yeni öğenin ad alanı URI'sini (varsa). String.Empty ve null eşdeğerdir.

Döndürülenler

Yeni XmlElement.

Örnekler

Aşağıdaki örnek, varolan XML belgesine yeni bir öğe ekler.

#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

Açıklamalar

Aşağıdaki C# kodu

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

aşağıdaki XML metnine eşdeğer bir öğe oluşturur:

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

Bu yöntem yeni nesneyi belge bağlamında oluştursa da, yeni nesneyi belge ağacına otomatik olarak eklemez. Yeni nesneyi eklemek için düğüm ekleme yöntemlerinden birini açıkça çağırmanız gerekir.

W3C Genişletilebilir Biçimlendirme Dili (XML) 1.0 önerisine göre, EntityReference bir Öznitelik düğümü dışında olduğunda, Belge ve Öğe düğümleri içinde ve EntityReference düğümlerinde Öğe düğümlerine izin verilir.

Bu yöntem, Belge Nesne Modeli'nin (DOM) Microsoft bir uzantısıdır.

Şunlara uygulanır