XmlDocument.CreateElement 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
创建 XmlElement。
重载
| 名称 | 说明 |
|---|---|
| CreateElement(String) |
创建具有指定名称的元素。 |
| CreateElement(String, String) |
创建一个 XmlElement 具有限定名和 NamespaceURI. |
| CreateElement(String, String, String) |
创建具有指定Prefix和 LocalNameNamespaceURI. |
CreateElement(String)
- Source:
- XmlDocument.cs
- Source:
- XmlDocument.cs
- 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;
using System.Xml;
public class Sample
{
public static void CreateTextNodeExample()
{
// Create the XmlDocument.
XmlDocument doc = new();
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);
}
}
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
此示例生成以下输出:
Display the modified XML...
<?xml version="1.0" encoding="utf-8"?>
<book genre="novel" ISBN="1-861001-57-5">
<title>Pride And Prejudice</title>
<price>19.95</price>
</book>
注解
请注意,返回的实例实现 XmlElement 接口,因此将直接在返回的对象上创建默认属性。
尽管此方法在文档上下文中创建新对象,但它不会自动将新对象添加到文档树。 若要添加新对象,必须显式调用其中一个节点插入方法。
根据 W3C 可扩展标记语言 (XML) 1.0 建议,在 Document 和 Element 节点中允许元素节点,当 EntityReference 节点不是 Attribute 节点的子节点时,在 EntityReference 节点中允许元素节点。
适用于
CreateElement(String, String)
- Source:
- XmlDocument.cs
- Source:
- XmlDocument.cs
- Source:
- XmlDocument.cs
- Source:
- XmlDocument.cs
- Source:
- XmlDocument.cs
创建一个 XmlElement 具有限定名和 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
参数
- 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 可扩展标记语言 (XML) 1.0 建议,在 Document 和 Element 节点中允许元素节点,当 EntityReference 节点不是 Attribute 节点的子节点时,在 EntityReference 节点中允许元素节点。
适用于
CreateElement(String, String, String)
- Source:
- XmlDocument.cs
- Source:
- XmlDocument.cs
- Source:
- XmlDocument.cs
- Source:
- XmlDocument.cs
- Source:
- XmlDocument.cs
创建具有指定Prefix和 LocalNameNamespaceURI.
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;
using System.IO;
using System.Xml;
public class Sample1
{
public static void CreateElementExample()
{
// Create the XmlDocument.
XmlDocument doc = new();
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 可扩展标记语言 (XML) 1.0 建议,在 Document 节点和元素节点中允许元素节点,在 EntityReference 节点外部时,允许在 EntityReference 节点中。
此方法是文档对象模型(DOM)的Microsoft扩展。