XmlDocument.CreateElement 方法

定义

创建 XmlElementCreates an XmlElement.

重载

CreateElement(String)

创建具有指定名称的元素。Creates an element with the specified name.

CreateElement(String, String)

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

CreateElement(String, String, String)

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

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

元素的限定名。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
End Class

注解

请注意,返回的实例实现 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)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)

创建具有限定名和 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

元素的限定名。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

元素的命名空间 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)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)

创建具有指定 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

新元素的前缀(如果有的话)。The prefix of the new element (if any). String.Empty 与 null 等效。String.Empty and null are equivalent.

localName
String

新元素的本地名称。The local name of the new element.

namespaceURI
String

新元素的命名空间 URI(如果有的话)。The namespace URI of the new element (if any). String.Empty 与 null 等效。String.Empty and null are equivalent.

返回

新的 XmlElementThe new XmlElement.

示例

下面的示例将一个新元素添加到现有的 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)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).

适用于