XmlDocument.CreateNode 方法

定义

创建 XmlNode

重载

CreateNode(String, String, String)

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

使用指定的节点类型创建一个 XmlNodeName以及 NamespaceURI

public:
 virtual System::Xml::XmlNode ^ CreateNode(System::String ^ nodeTypeString, System::String ^ name, System::String ^ namespaceURI);
public virtual System.Xml.XmlNode CreateNode(string nodeTypeString, string name, string namespaceURI);
public virtual System.Xml.XmlNode CreateNode(string nodeTypeString, string name, string? namespaceURI);
abstract member CreateNode : string * string * string -> System.Xml.XmlNode
override this.CreateNode : string * string * string -> System.Xml.XmlNode
Public Overridable Function CreateNode (nodeTypeString As String, name As String, namespaceURI As String) As XmlNode

参数

nodeTypeString
String

新节点的 XmlNodeType 字符串版本。 此参数必须是下表中列出的值之一。

name
String

新节点的限定名称。 如果名称包含冒号,则会将其解析为 Prefix 组件和 LocalName 组件。

namespaceURI
String

新节点的命名空间 URI。

返回

新的 XmlNode

例外

未提供该名称,并且 XmlNodeType 需要一个名称;或者 nodeTypeString 不是下面列出的字符串之一。

示例

以下示例创建一个新元素并将其插入文档中。

using System;
using System.Xml;

public class Sample {

  public static void Main() {

       XmlDocument doc = new XmlDocument();
       doc.LoadXml("<book>" +
                   "  <title>Oberon's Legacy</title>" +
                   "  <price>5.95</price>" +
                   "</book>");

       // Create a new element node.
       XmlNode newElem = doc.CreateNode("element", "pages", "");
       newElem.InnerText = "290";

       Console.WriteLine("Add the new element to the document...");
       XmlElement root = doc.DocumentElement;
       root.AppendChild(newElem);

       Console.WriteLine("Display the modified XML document...");
       Console.WriteLine(doc.OuterXml);
   }
 }
Imports System.Xml

public class Sample 

  public shared sub Main() 

       Dim doc as XmlDocument = new XmlDocument()
       doc.LoadXml("<book>" & _
                   "  <title>Oberon's Legacy</title>" & _
                   "  <price>5.95</price>" & _
                   "</book>") 
 
       ' Create a new element node.
       Dim newElem as XmlNode = doc.CreateNode("element", "pages", "")  
       newElem.InnerText = "290"
     
       Console.WriteLine("Add the new element to the document...")
       Dim root as XmlElement = doc.DocumentElement
       root.AppendChild(newElem)
     
       Console.WriteLine("Display the modified XML document...")
       Console.WriteLine(doc.OuterXml)
   end sub
end class

注解

nodeTypeString 参数区分大小写,并且必须是下表中的值之一。

nodeTypeString XmlNodeType
属性 Attribute
cdatasection CDATA
注释 评论
文档 Document
documentfragment DocumentFragment
documenttype 文档类型
元素 元素
entityreference EntityReference
processinginstruction 处理指令
significantwhitespace 显著空白符
文本消息 文本
空格 空格

尽管此方法在文档上下文中创建新对象,但它不会自动将新对象添加到文档树。 若要添加新对象,必须显式调用其中一个节点插入方法。

下表根据 W3C 可扩展标记语言 (XML) 1.0 建议,允许在另一个 NodeType[column] 内允许哪些 NodeType[row]。

节点类型 Document 文档类型 XML声明 元素 Attribute 文本 CDATA 标记 EntityReference
Document
DocumentType 是的
XmlDeclaration 是的*
Element 是的 是的 是†
Attribute 的。
Text 是的 是的 是的
CDATA 是的 是†
Markup § 是的 是的
EntityReference 是的 是的 是的

* 节点 XmlDeclaration 必须是 Document 节点的第一个子级。

§ Markup 包括 ProcessingInstructionComment 节点。

Element仅当节点不是节点的CDATA子节点时EntityReference,才允许EntityReference†节点和Attribute节点。

• 特性不是节点的 Element 子级。 属性包含在属于 Element 节点的属性集合中。

此方法是文档对象模型(DOM)的Microsoft扩展。

适用于

CreateNode(XmlNodeType, String, String)

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

XmlNode使用指定的XmlNodeTypeNameNamespaceURI

public:
 virtual System::Xml::XmlNode ^ CreateNode(System::Xml::XmlNodeType type, System::String ^ name, System::String ^ namespaceURI);
public virtual System.Xml.XmlNode CreateNode(System.Xml.XmlNodeType type, string name, string namespaceURI);
public virtual System.Xml.XmlNode CreateNode(System.Xml.XmlNodeType type, string name, string? namespaceURI);
abstract member CreateNode : System.Xml.XmlNodeType * string * string -> System.Xml.XmlNode
override this.CreateNode : System.Xml.XmlNodeType * string * string -> System.Xml.XmlNode
Public Overridable Function CreateNode (type As XmlNodeType, name As String, namespaceURI As String) As XmlNode

参数

type
XmlNodeType

XmlNodeType新节点。

name
String

新节点的限定名称。 如果名称包含冒号,则会将其解析为 Prefix 组件和 LocalName 组件。

namespaceURI
String

新节点的命名空间 URI。

返回

新的 XmlNode

例外

未提供该名称, XmlNodeType 并且需要一个名称。

示例

以下示例创建一个新元素并将其插入 XML 文档中。

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.
    XmlNode elem = doc.CreateNode(XmlNodeType.Element, "price", null);
    elem.InnerText = "19.95";
    doc.DocumentElement.AppendChild(elem);

    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.
        Dim elem As XmlNode = doc.CreateNode(XmlNodeType.Element, "price", Nothing)
        elem.InnerText = "19.95"
        doc.DocumentElement.AppendChild(elem)
        
        Console.WriteLine("Display the modified XML...")
        doc.Save(Console.Out)
    End Sub
End Class

注解

尽管此方法在文档上下文中创建新对象,但它不会自动将新对象添加到文档树。 若要添加新对象,必须显式调用其中一个节点插入方法。

下表根据 W3C 可扩展标记语言 (XML) 1.0 建议,允许在另一个 NodeType[column] 内允许哪些 NodeType[row]。

节点类型 Document 文档类型 XML声明 元素 Attribute 文本 CDATA 标记 EntityReference
Document
DocumentType 是的
XmlDeclaration 是的*
Element 是的 是的 是†
Attribute 是的。
Text 是的 是的 是的
CDATA 是的 是†
Markup§ 是的 是的
EntityReference 是的 是的 是的

* 节点 XmlDeclaration 必须是 Document 节点的第一个子级。

§ Markup 包括 ProcessingInstructionComment 节点。

Element仅当节点不是节点的CDATA子节点时EntityReference,才允许EntityReference†节点和Attribute节点。

• 特性不是节点的 Element 子级。 属性包含在属于 Element 节点的属性集合中。

此方法是文档对象模型(DOM)的Microsoft扩展。

适用于

CreateNode(XmlNodeType, String, String, String)

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

XmlNode使用指定的 XmlNodeTypePrefixNameNamespaceURI

public:
 virtual System::Xml::XmlNode ^ CreateNode(System::Xml::XmlNodeType type, System::String ^ prefix, System::String ^ name, System::String ^ namespaceURI);
public virtual System.Xml.XmlNode CreateNode(System.Xml.XmlNodeType type, string prefix, string name, string namespaceURI);
public virtual System.Xml.XmlNode CreateNode(System.Xml.XmlNodeType type, string? prefix, string name, string? namespaceURI);
abstract member CreateNode : System.Xml.XmlNodeType * string * string * string -> System.Xml.XmlNode
override this.CreateNode : System.Xml.XmlNodeType * string * string * string -> System.Xml.XmlNode
Public Overridable Function CreateNode (type As XmlNodeType, prefix As String, name As String, namespaceURI As String) As XmlNode

参数

type
XmlNodeType

XmlNodeType新节点。

prefix
String

新节点的前缀。

name
String

新节点的本地名称。

namespaceURI
String

新节点的命名空间 URI。

返回

新的 XmlNode

例外

未提供该名称, XmlNodeType 并且需要一个名称。

示例

以下示例向文档添加新元素。

using System;
using System.Xml;

public class Sample {

  public static void Main() {

        // Create a new document containing information about a book
        XmlDocument doc = new XmlDocument();
        doc.LoadXml("<book>" +
                    "  <title>Oberon's Legacy</title>" +
                    "  <price>5.95</price>" +
                    "</book>");

        // Create a new element node for the ISBN of the book
        // It is possible to supply a prefix for this node, and specify a qualified namespace.
        XmlNode newElem;
        newElem = doc.CreateNode(XmlNodeType.Element, "g", "ISBN", "https://global.ISBN/list");
        newElem.InnerText = "1-861001-57-5";

        // Add the new element to the document
        XmlElement root = doc.DocumentElement;
        root.AppendChild(newElem);

        // Display the modified XML document
        Console.WriteLine(doc.OuterXml);

        //Output:
        // <book><title>Oberon's Legacy</title><price>5.95</price><g:ISBN xmlns:g="https://global.ISBN/list">1-861001-57-5</g:ISBN></book>
   }
 }
Imports System.Xml

public class Sample 

  public shared sub Main() 

        Dim doc as XmlDocument = new XmlDocument()
        doc.LoadXml("<book>" & _
                    "  <title>Oberon's Legacy</title>" & _
                    "  <price>5.95</price>" & _
                       "</book>") 
 
        ' Create a new element node.
        ' It is possible to supply a prefix for this node, and specify a qualified namespace
        Dim newElem as XmlNode
        newElem = doc.CreateNode(XmlNodeType.Element,"g", "ISBN","https://global.ISBN/list")
        newElem.InnerText = "1-861001-57-5"
     
        ' Add the new element to the document
        Dim root as XmlElement = doc.DocumentElement
        root.AppendChild(newElem)
     
        ' Display the modified XML document
        Console.WriteLine(doc.OuterXml)
        
        ' Output:
        ' <book><title>Oberon's Legacy</title><price>5.95</price><g:ISBN xmlns:g="https://global.ISBN/list">1-861001-57-5</g:ISBN></book>
   end sub
end class

注解

尽管此方法在文档上下文中创建新对象,但它不会自动将新对象添加到文档树。 若要添加新对象,必须显式调用其中一个节点插入方法。

下表根据 W3C 可扩展标记语言 (XML) 1.0 建议,允许在另一个 NodeType[column] 内允许哪些 NodeType[row]。

Document 文档类型 XML声明 元素 Attribute 文本 CDATA 标记 EntityReference
Document
DocumentType 是的
XmlDeclaration 是的*
Element 是的 是的 是†
Attribute 是的。
Text 是的 是的 是的
CDATA 是的 是†
Markup§ 是的 是的
EntityReference 是的 是的 是的

* XmlDeclaration 节点必须是 Document 节点的第一个子级。

§ Markup 包括 ProcessingInstructionComment 节点。

Element仅当节点不是节点的CDATA子节点时EntityReference,才允许EntityReference†节点和Attribute节点。

• 特性不是节点的 Element 子级。 属性包含在属于 Element 节点的属性集合中。

此方法是文档对象模型(DOM)的Microsoft扩展。

适用于