XmlDocument.CreateElement 方法

定義

建立 XmlElement

多載

CreateElement(String)

建立具有指定名稱的項目。

CreateElement(String, String)

建立具有限定名稱和 NamespaceURIXmlElement

CreateElement(String, String, String)

建立具有指定之 PrefixLocalNameNamespaceURI 的元素。

CreateElement(String)

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

建立具有指定名稱的項目。

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

項目的限定名稱。 如果名稱包含冒號,Prefix 屬性會反映冒號之前的名稱部分,而 LocalName 屬性會反映冒號之後的名稱部分。 限定名稱不能包含 'xmlns' 前置詞。

傳回

新的 XmlElement

範例

下列範例會建立新的 元素,並將其新增至檔。

#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

備註

請注意,傳回的 實例會 XmlElement 實作 介面,因此預設屬性會直接在傳回的物件上建立。

雖然這個方法會在檔的內容中建立新的 物件,但不會自動將新物件加入檔樹狀結構。 若要新增物件,您必須明確呼叫其中一個節點插入方法。

根據 W3C Extensible Markup Language (XML) 1.0 建議,在 Document 和 Element 節點內允許元素節點,以及 EntityReference 節點中當 EntityReference 節點不是 Attribute 節點的子節點時。

適用於

CreateElement(String, String)

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

建立具有限定名稱和 NamespaceURIXmlElement

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

參數

qualifiedName
String

項目的限定名稱。 如果名稱包含冒號,Prefix 屬性會反映冒號前面的名稱部分,而 LocalName 屬性會反映冒號後面的名稱部分。 限定名稱不能包含 'xmlns' 前置詞。

namespaceURI
String

項目的命名空間 URI。

傳回

新的 XmlElement

備註

下列 C# 程式碼

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

會產生相當於下列 XML 文字的專案。

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

雖然這個方法會在檔的內容中建立新的 物件,但不會自動將新物件加入檔樹狀結構。 若要新增物件,您必須明確呼叫其中一個節點插入方法。

根據 W3C Extensible Markup Language (XML) 1.0 建議,在 Document 和 Element 節點內允許元素節點,以及 EntityReference 節點中當 EntityReference 節點不是 Attribute 節點的子節點時。

適用於

CreateElement(String, String, String)

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

建立具有指定之 PrefixLocalNameNamespaceURI 的元素。

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

參數

prefix
String

新項目的前置詞 (如有此項)。 String.Empty 與 null 相等。

localName
String

新項目的本機名稱。

namespaceURI
String

新項目的命名空間 URI (如有此項)。 String.Empty 與 null 相等。

傳回

新的 XmlElement

範例

下列範例會將新的 專案加入至現有的 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

備註

下列 C# 程式碼

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

會建立相當於下列 XML 文字的專案:

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

雖然這個方法會在檔的內容中建立新的 物件,但不會自動將新物件加入檔樹狀結構。 若要新增物件,您必須明確呼叫其中一個節點插入方法。

根據 W3C Extensible Markup Language (XML) 1.0 建議,在 Document 和 Element 節點內允許元素節點,以及在 EntityReference 節點中當 EntityReference 位於 Attribute 節點外部時。

這個方法是檔物件模型 (DOM) Microsoft延伸模組。

適用於