XmlDocument.CreateNode 方法   
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
创建 XmlNode。
重载
| CreateNode(String, String, String) | 创建具有指定的节点类型、Name 和 NamespaceURI 的 XmlNode。 | 
| CreateNode(XmlNodeType, String, String) | 创建一个具有指定的 XmlNodeType、Name 和 NamespaceURI 的 XmlNode。 | 
| CreateNode(XmlNodeType, String, String, String) | 创建一个具有指定的XmlNodeType、Prefix、Name 和 NamespaceURI 的 XmlNode。 | 
CreateNode(String, String, String)
- Source:
- XmlDocument.cs
- Source:
- XmlDocument.cs
- Source:
- XmlDocument.cs
创建具有指定的节点类型、Name 和 NamespaceURI 的 XmlNode。
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.XmlNodePublic Overridable Function CreateNode (nodeTypeString As String, name As String, namespaceURI As String) As XmlNode参数
- nodeTypeString
- String
新节点的 XmlNodeType 的字符串版本。 该参数必须是下表中列出的值之一。
- namespaceURI
- String
新节点的命名空间 URI。
返回
新的 XmlNode。
例外
未提供名称,而 XmlNodeType 需要名称;或者 nodeTypeString 不是下列字符串之一。
示例
以下示例创建一个新元素并将其插入文档中。
#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 区分大小写,必须是下表中的值之一。
| nodeTypeString | XmlNodeType | 
|---|---|
| attribute | Attribute | 
| cdatasection | CDATA | 
| comment | 评论 | 
| 文档 | 文档 | 
| documentfragment | DocumentFragment | 
| documenttype | DocumentType | 
| element | 元素 | 
| entityreference | EntityReference | 
| processinginstruction | ProcessingInstruction | 
| significantwhitespace | SignificantWhitespace | 
| text | 文本 | 
| whitespace | 空格 | 
尽管此方法在文档的上下文中创建新对象,但它不会自动将新对象添加到文档树。 若要添加新对象,必须显式调用节点插入方法之一。
下表显示了根据 W3C 可扩展标记语言 (XML) 1.0 建议,允许在另一个 NodeType[列]内使用哪些 NodeType[row]。
| 文档 | DocumentType | XmlDeclaration | 元素 | Attribute | 文本 | CDATA | 标记 | EntityReference | |
|---|---|---|---|---|---|---|---|---|---|
| Document | 否 | 否 | 否 | 否 | 否 | 否 | 否 | 否 | 否 | 
| DocumentType | 是 | 否 | 否 | 否 | 否 | 否 | 否 | 否 | 否 | 
| XmlDeclaration | 是* | 否 | 否 | 否 | 否 | 否 | 否 | 否 | 否 | 
| Element | 是 | 否 | 否 | 是 | 否 | 否 | 否 | 否 | 是的*** | 
| Attribute | 否 | 否 | 否 | 是的**** | 否 | 否 | 否 | 否 | 否 | 
| Text | 否 | 否 | 否 | 是 | 是 | 否 | 否 | 否 | 是 | 
| CDATA | 否 | 否 | 否 | 是 | 否 | 否 | 否 | 否 | 是的*** | 
| Markup** | 是 | 否 | 否 | 是 | 否 | 否 | 否 | 否 | 否 | 
| EntityReference | 否 | 否 | 否 | 是 | 是 | 否 | 否 | 否 | 是 | 
* XmlDeclaration 节点必须是 Document 节点的第一个子节点。
** 标记包括 ProcessingInstruction 和 Comment 节点。
只有在 EntityReference 节点不是 Attribute 节点的子节点时,才允许在 EntityReference 节点中使用元素和 CDATA 节点。
属性不是元素节点的子级。 属性包含在属于 Element 节点的属性集合中。
此方法是文档对象模型 (DOM) 的Microsoft扩展。
适用于
CreateNode(XmlNodeType, String, String)
- Source:
- XmlDocument.cs
- Source:
- XmlDocument.cs
- Source:
- XmlDocument.cs
创建一个具有指定的 XmlNodeType、Name 和 NamespaceURI 的 XmlNode。
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.XmlNodePublic Overridable Function CreateNode (type As XmlNodeType, name As String, namespaceURI As String) As XmlNode参数
- type
- XmlNodeType
新节点的 XmlNodeType。
- namespaceURI
- String
新节点的命名空间 URI。
返回
新的 XmlNode。
例外
未提供名称,而 XmlNodeType 需要名称。
示例
以下示例创建一个新元素并将其插入 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;
   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
注解
尽管此方法在文档的上下文中创建新对象,但它不会自动将新对象添加到文档树。 若要添加新对象,必须显式调用节点插入方法之一。
下表显示了根据 W3C 可扩展标记语言 (XML) 1.0 建议,允许在另一个 NodeType[列]内使用哪些 NodeType[row]。
| 文档 | DocumentType | XmlDeclaration | 元素 | Attribute | 文本 | CDATA | 标记 | EntityReference | |
|---|---|---|---|---|---|---|---|---|---|
| Document | 否 | 否 | 否 | 否 | 否 | 否 | 否 | 否 | 否 | 
| DocumentType | 是 | 否 | 否 | 否 | 否 | 否 | 否 | 否 | 否 | 
| XmlDeclaration | 是* | 否 | 否 | 否 | 否 | 否 | 否 | 否 | 否 | 
| Element | 是 | 否 | 否 | 是 | 否 | 否 | 否 | 否 | 是的*** | 
| Attribute | 否 | 否 | 否 | 是的**** | 否 | 否 | 否 | 否 | 否 | 
| Text | 否 | 否 | 否 | 是 | 是 | 否 | 否 | 否 | 是 | 
| CDATA | 否 | 否 | 否 | 是 | 否 | 否 | 否 | 否 | 是的*** | 
| Markup** | 是 | 否 | 否 | 是 | 否 | 否 | 否 | 否 | 否 | 
| EntityReference | 否 | 否 | 否 | 是 | 是 | 否 | 否 | 否 | 是 | 
* XmlDeclaration 节点必须是 Document 节点的第一个子节点。
** 标记包括 ProcessingInstruction 和 Comment 节点。
仅当 EntityReference 节点不是 Attribute 节点的子级时,EntityReference 节点才允许使用元素和 CDATA 节点。
属性不是元素节点的子级。 属性包含在属于 Element 节点的属性集合中。
此方法是文档对象模型 (DOM) 的Microsoft扩展。
适用于
CreateNode(XmlNodeType, String, String, String)
- Source:
- XmlDocument.cs
- Source:
- XmlDocument.cs
- Source:
- XmlDocument.cs
创建一个具有指定的XmlNodeType、Prefix、Name 和 NamespaceURI 的 XmlNode。
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.XmlNodePublic 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.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
注解
尽管此方法在文档的上下文中创建新对象,但它不会自动将新对象添加到文档树。 若要添加新对象,必须显式调用节点插入方法之一。
下表显示了根据 W3C 可扩展标记语言 (XML) 1.0 建议,允许在另一个 NodeType[列]内使用哪些 NodeType[row]。
| 文档 | DocumentType | XmlDeclaration | 元素 | Attribute | 文本 | CDATA | 标记 | EntityReference | |
|---|---|---|---|---|---|---|---|---|---|
| Document | 否 | 否 | 否 | 否 | 否 | 否 | 否 | 否 | 否 | 
| DocumentType | 是 | 否 | 否 | 否 | 否 | 否 | 否 | 否 | 否 | 
| XmlDeclaration | 是* | 否 | 否 | 否 | 否 | 否 | 否 | 否 | 否 | 
| Element | 是 | 否 | 否 | 是 | 否 | 否 | 否 | 否 | 是的*** | 
| Attribute | 否 | 否 | 否 | 是的**** | 否 | 否 | 否 | 否 | 否 | 
| Text | 否 | 否 | 否 | 是 | 是 | 否 | 否 | 否 | 是 | 
| CDATA | 否 | 否 | 否 | 是 | 否 | 否 | 否 | 否 | 是的*** | 
| Markup** | 是 | 否 | 否 | 是 | 否 | 否 | 否 | 否 | 否 | 
| EntityReference | 否 | 否 | 否 | 是 | 是 | 否 | 否 | 否 | 是 | 
* XmlDeclaration 节点必须是 Document 节点的第一个子节点。
** 标记包括 ProcessingInstruction 和 Comment 节点。
仅当 EntityReference 节点不是 Attribute 节点的子级时,EntityReference 节点才允许使用元素和 CDATA 节点。
属性不是元素节点的子级。 属性包含在属于 Element 节点的属性集合中。
此方法是文档对象模型 (DOM) 的Microsoft扩展。