XmlDocument.CreateElement XmlDocument.CreateElement XmlDocument.CreateElement XmlDocument.CreateElement Method

定義

建立 XmlElementCreates an XmlElement.

多載

CreateElement(String) CreateElement(String) CreateElement(String) CreateElement(String)

建立具有指定名稱的項目。Creates an element with the specified name.

CreateElement(String, String) CreateElement(String, String) CreateElement(String, String) CreateElement(String, String)

建立具有限定名稱和 NamespaceURIXmlElementCreates an XmlElement with the qualified name and NamespaceURI.

CreateElement(String, String, String) CreateElement(String, String, String) CreateElement(String, String, String) CreateElement(String, String, String)

建立具有指定之 PrefixLocalNameNamespaceURI 的元素。Creates an element with the specified Prefix, LocalName, and NamespaceURI.

CreateElement(String) CreateElement(String) CreateElement(String) CreateElement(String)

建立具有指定名稱的項目。Creates an element with the specified name.

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

參數

name
String String String String

項目的限定名稱。The qualified name of the element. 如果名稱包含冒號,Prefix 屬性會反映冒號之前的名稱部分,而 LocalName 屬性會反映冒號之後的名稱部分。If the name contains a colon then the Prefix property reflects the part of the name preceding the colon and the LocalName property reflects the part of the name after the colon. 限定名稱不能包含 'xmlns' 前置詞。The qualified name cannot include a prefix of 'xmlns'.

傳回

新的 XmlElementThe new XmlElement.

範例

下列範例會建立新的專案, 並將其新增至檔。The following example creates a new element and adds it to the document.

#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 'Main 
End Class 'Sample

備註

請注意, 傳回XmlElement的實例會執行介面, 因此會在傳回的物件上直接建立預設屬性。Note that the instance returned implements the XmlElement interface, so default attributes would be created directly on the returned object.

雖然這個方法會在檔的內容中建立新的物件, 但它不會自動將新的物件加入至檔樹狀結構。Although this method creates the new object in the context of the document, it does not automatically add the new object to the document tree. 若要加入新的物件, 您必須明確地呼叫其中一個節點插入方法。To add the new object, you must explicitly call one of the node insert methods.

根據 W3C可延伸標記語言 (XML) (XML) 1.0 建議事項, 當 entityreference 節點不是屬性節點的子系時, 會在檔和專案節點和 entityreference 節點中允許元素節點。According to the W3C Extensible Markup Language (XML) 1.0 recommendation, Element nodes are allowed within Document and Element nodes, and in EntityReference nodes when the EntityReference node is not a child of an Attribute node.

CreateElement(String, String) CreateElement(String, String) CreateElement(String, String) CreateElement(String, String)

建立具有限定名稱和 NamespaceURIXmlElementCreates an XmlElement with the qualified name and 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

參數

qualifiedName
String String String String

項目的限定名稱。The qualified name of the element. 如果名稱包含冒號,Prefix 屬性會反映冒號前面的名稱部分,而 LocalName 屬性會反映冒號後面的名稱部分。If the name contains a colon then the Prefix property will reflect the part of the name preceding the colon and the LocalName property will reflect the part of the name after the colon. 限定名稱不能包含 'xmlns' 前置詞。The qualified name cannot include a prefix of 'xmlns'.

namespaceURI
String String String String

項目的命名空間 URI。The namespace URI of the element.

傳回

新的 XmlElementThe new XmlElement.

備註

下列C#程式碼The following C# code

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

產生相當於下列 XML 文字的元素。results in an element that is equivalent to the following XML text.

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

雖然這個方法會在檔的內容中建立新的物件, 但它不會自動將新的物件加入至檔樹狀結構。Although this method creates the new object in the context of the document, it does not automatically add the new object to the document tree. 若要加入新的物件, 您必須明確地呼叫其中一個節點插入方法。To add the new object, you must explicitly call one of the node insert methods.

根據 W3C可延伸標記語言 (XML) (XML) 1.0 建議事項, 當 entityreference 節點不是屬性節點的子系時, 會在檔和專案節點和 entityreference 節點中允許元素節點。According to the W3C Extensible Markup Language (XML) 1.0 recommendation, Element nodes are allowed within Document and Element nodes, and in EntityReference nodes when the EntityReference node is not a child of an Attribute node.

CreateElement(String, String, String) CreateElement(String, String, String) CreateElement(String, String, String) CreateElement(String, String, String)

建立具有指定之 PrefixLocalNameNamespaceURI 的元素。Creates an element with the specified Prefix, LocalName, and 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);
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

參數

prefix
String String String String

新項目的前置詞 (如有此項)。The prefix of the new element (if any). String.Empty 與 null 相等。String.Empty and null are equivalent.

localName
String String String String

新項目的本機名稱。The local name of the new element.

namespaceURI
String String String String

新項目的命名空間 URI (如有此項)。The namespace URI of the new element (if any). String.Empty 與 null 相等。String.Empty and null are equivalent.

傳回

範例

下列範例會將新的專案加入至現有的 XML 檔。The following example adds a new element to the existing XML document.

#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

備註

下列C#程式碼The following C# code

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

建立相當於下列 XML 文字的元素:creates an element equivalent to the following XML text:

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

雖然這個方法會在檔的內容中建立新的物件, 但它不會自動將新的物件加入至檔樹狀結構。Although this method creates the new object in the context of the document, it does not automatically add the new object to the document tree. 若要加入新的物件, 您必須明確地呼叫其中一個節點插入方法。To add the new object, you must explicitly call one of the node insert methods.

根據 W3C可延伸標記語言 (XML) (XML) 1.0 建議事項, 在檔和專案節點中, 以及在 Entityreference 位於屬性節點之外時, 會在 entityreference 節點中允許元素節點。According to the W3C Extensible Markup Language (XML) 1.0 recommendation, Element nodes are allowed within Document and Element nodes, and in EntityReference nodes when the EntityReference is outside an Attribute node.

這個方法是檔物件模型 (DOM) 的 Microsoft 擴充功能。This method is a Microsoft extension to the Document Object Model (DOM).

適用於