XmlDocument.CreateNode 方法

定义

创建 XmlNodeCreates an XmlNode.

重载

CreateNode(String, String, String)

创建具有指定的节点类型、NameNamespaceURIXmlNodeCreates an XmlNode with the specified node type, Name, and NamespaceURI.

CreateNode(XmlNodeType, String, String)

创建一个具有指定的 XmlNodeTypeNameNamespaceURIXmlNodeCreates an XmlNode with the specified XmlNodeType, Name, and NamespaceURI.

CreateNode(XmlNodeType, String, String, String)

创建一个具有指定的XmlNodeTypePrefixNameNamespaceURIXmlNodeCreates a XmlNode with the specified XmlNodeType, Prefix, Name, and NamespaceURI.

CreateNode(String, String, String)

创建具有指定的节点类型、NameNamespaceURIXmlNodeCreates an XmlNode with the specified node type, Name, and 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 的字符串版本。String version of the XmlNodeType of the new node. 该参数必须是下表中列出的值之一。This parameter must be one of the values listed in the table below.

name
String

新节点的限定名称。The qualified name of the new node. 如果名称包含一个冒号,则将它解析为 PrefixLocalName 两部分。If the name contains a colon, it is parsed into Prefix and LocalName components.

namespaceURI
String

新节点的命名空间 URI。The namespace URI of the new node.

返回

XmlNode

新的 XmlNodeThe new XmlNode.

例外

未提供名称,而 XmlNodeType 需要名称;或者 nodeTypeString 不是下列字符串之一。The name was not provided and the XmlNodeType requires a name; or nodeTypeString is not one of the strings listed below.

示例

下面的示例创建一个新元素并将其插入到文档中。The following example creates a new element and inserts it into the document.

#using <System.Xml.dll>

using namespace System;
using namespace System::Xml;
int main()
{
   XmlDocument^ doc = gcnew 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 );
}

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参数区分大小写,并且必须是下表中的值之一。The nodeTypeString parameter is case sensitive and must be one of the values in the following table.

nodeTypeStringnodeTypeString XmlNodeTypeXmlNodeType
attributeattribute AttributeAttribute
cdatasectioncdatasection CDATACDATA
commentcomment 评论Comment
文档document 文档Document
documentfragmentdocumentfragment DocumentFragmentDocumentFragment
documenttypedocumenttype DocumentTypeDocumentType
elementelement 元素Element
entityreferenceentityreference EntityReferenceEntityReference
processinginstructionprocessinginstruction ProcessingInstructionProcessingInstruction
significantwhitespacesignificantwhitespace SignificantWhitespaceSignificantWhitespace
texttext 文本Text
whitespacewhitespace 空格Whitespace

尽管此方法会在文档的上下文中创建新的对象,但它不会自动将新对象添加到文档树。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 建议,另一个 nodetype [column] 内允许的 nodetype [row] 的内容。The following table shows you what NodeType[row] is allowed inside another NodeType[column] according to the W3C Extensible Markup Language (XML) 1.0 recommendation.

文档Document DocumentTypeDocumentType XmlDeclarationXmlDeclaration 元素Element AttributeAttribute 文本Text CDATACDATA 标记Markup EntityReferenceEntityReference
Document no no no no no no no no no
DocumentType yes no no no no no no no no
XmlDeclaration 是*yes* no no no no no no no no
Element yes no no yes no no no no 是 * * *yes***
Attribute no no no 是 * * * *yes**** no no no no no
Text no no no yes yes no no no yes
CDATA no no no yes no no no no 是 * * *yes***
Markup** yes no no yes no no no no no
EntityReference no no no yes yes no no no yes

* XmlDeclaration 节点必须是文档节点的第一个子级。* The XmlDeclaration node must be the first child of the Document node.

* * 标记包括 ProcessingInstruction 和注释节点。** Markup includes ProcessingInstruction and Comment nodes.

仅当 EntityReference 节点不是属性节点的子节点时,才允许在 EntityReference 节点中使用元素和 CDATA 节点。*** Element and CDATA nodes are only allowed in EntityReference nodes when the EntityReference node is not a child of an Attribute node.

特性不是元素节点的子项。**** Attributes are not children of an Element node. 特性包含在属于元素节点的特性集合中。Attributes are contained inside an attribute collection that belongs to an Element node.

此方法是文档对象模型 (DOM) 的 Microsoft 扩展。This method is a Microsoft extension to the Document Object Model (DOM).

适用于

CreateNode(XmlNodeType, String, String)

创建一个具有指定的 XmlNodeTypeNameNamespaceURIXmlNodeCreates an XmlNode with the specified XmlNodeType, Name, and NamespaceURI.

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

新节点的 XmlNodeTypeThe XmlNodeType of the new node.

name
String

新节点的限定名称。The qualified name of the new node. 如果名称包含一个冒号,则将其解析为 PrefixLocalName 两部分。If the name contains a colon then it is parsed into Prefix and LocalName components.

namespaceURI
String

新节点的命名空间 URI。The namespace URI of the new node.

返回

XmlNode

新的 XmlNodeThe new XmlNode.

例外

未提供名称,而 XmlNodeType 需要名称。The name was not provided and the XmlNodeType requires a name.

示例

下面的示例创建一个新元素并将其插入到 XML 文档中。The following example creates a new element and inserts it into an 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;
   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", nullptr );
   elem->InnerText = "19.95";
   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();
    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

注解

尽管此方法会在文档的上下文中创建新的对象,但它不会自动将新对象添加到文档树。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 建议,另一个 nodetype [column] 内允许的 nodetype [row] 的内容。The following table shows you what NodeType[row] is allowed inside another NodeType[column] according to the W3C Extensible Markup Language (XML) 1.0 recommendation.

文档Document DocumentTypeDocumentType XmlDeclarationXmlDeclaration 元素Element AttributeAttribute 文本Text CDATACDATA 标记Markup EntityReferenceEntityReference
Document no no no no no no no no no
DocumentType yes no no no no no no no no
XmlDeclaration 是*yes* no no no no no no no no
Element yes no no yes no no no no 是 * * *yes***
Attribute no no no 是 * * * *yes**** no no no no no
Text no no no yes yes no no no yes
CDATA no no no yes no no no no 是 * * *yes***
Markup** yes no no yes no no no no no
EntityReference no no no yes yes no no no yes

* XmlDeclaration 节点必须是文档节点的第一个子级。* The XmlDeclaration node must be the first child of the Document node.

* * 标记包括 ProcessingInstruction 和注释节点。** Markup includes ProcessingInstruction and Comment nodes.

仅当 EntityReference 节点不是属性节点的子节点时,才允许在 EntityReference 节点中使用元素和 CDATA 节点。*** Element and CDATA nodes are only allowed in EntityReference nodes when the EntityReference node is not a child of an Attribute node.

特性不是元素节点的子项。**** Attributes are not children of an Element node. 特性包含在属于元素节点的特性集合中。Attributes are contained inside an attribute collection that belongs to an Element node.

此方法是文档对象模型 (DOM) 的 Microsoft 扩展。This method is a Microsoft extension to the Document Object Model (DOM).

适用于

CreateNode(XmlNodeType, String, String, String)

创建一个具有指定的XmlNodeTypePrefixNameNamespaceURIXmlNodeCreates a XmlNode with the specified XmlNodeType, Prefix, Name, and NamespaceURI.

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

新节点的 XmlNodeTypeThe XmlNodeType of the new node.

prefix
String

新节点的前缀。The prefix of the new node.

name
String

新节点的本地名称。The local name of the new node.

namespaceURI
String

新节点的命名空间 URI。The namespace URI of the new node.

返回

XmlNode

新的 XmlNodeThe new XmlNode.

例外

未提供名称,而 XmlNodeType 需要名称。The name was not provided and the XmlNodeType requires a name.

示例

下面的示例将一个新元素添加到文档中。The following example adds a new element to the document.

#using <System.Xml.dll>

using namespace System;
using namespace System::Xml;
int main()
{
   XmlDocument^ doc = gcnew XmlDocument;
   doc->LoadXml( "<book>  <title>Oberon's Legacy</title>  <price>5.95</price></book>" );
   
   // Create a new element node.
   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>
}

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

注解

尽管此方法会在文档的上下文中创建新的对象,但它不会自动将新对象添加到文档树。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 建议,另一个 nodetype [column] 内允许的 nodetype [row] 的内容。The following table shows you what NodeType[row] is allowed inside another NodeType[column] according to the W3C Extensible Markup Language (XML) 1.0 recommendation.

文档Document DocumentTypeDocumentType XmlDeclarationXmlDeclaration 元素Element AttributeAttribute 文本Text CDATACDATA 标记Markup EntityReferenceEntityReference
Document no no no no no no no no no
DocumentType yes no no no no no no no no
XmlDeclaration 是*yes* no no no no no no no no
Element yes no no yes no no no no 是 * * *yes***
Attribute no no no 是 * * * *yes**** no no no no no
Text no no no yes yes no no no yes
CDATA no no no yes no no no no 是 * * *yes***
Markup** yes no no yes no no no no no
EntityReference no no no yes yes no no no yes

* XmlDeclaration 节点必须是文档节点的第一个子级。* The XmlDeclaration node must be the first child of the Document node.

* * 标记包括 ProcessingInstruction 和注释节点。** Markup includes ProcessingInstruction and Comment nodes.

仅当 EntityReference 节点不是属性节点的子节点时,才允许在 EntityReference 节点中使用元素和 CDATA 节点。*** Element and CDATA nodes are only allowed in EntityReference nodes when the EntityReference node is not a child of an Attribute node.

特性不是元素节点的子项。**** Attributes are not children of an Element node. 特性包含在属于元素节点的特性集合中。Attributes are contained inside an attribute collection that belongs to the Element node.

此方法是文档对象模型 (DOM) 的 Microsoft 扩展。This method is a Microsoft extension to the Document Object Model (DOM).

适用于