XmlDocument.CreateNode XmlDocument.CreateNode XmlDocument.CreateNode XmlDocument.CreateNode Method

定義

建立 XmlNodeCreates an XmlNode.

多載

CreateNode(String, String, String) CreateNode(String, String, String) CreateNode(String, String, String) CreateNode(String, String, String)

建立具有指定節點類型、NameNamespaceURIXmlNodeCreates an XmlNode with the specified node type, Name, and NamespaceURI.

CreateNode(XmlNodeType, String, String) CreateNode(XmlNodeType, String, String) CreateNode(XmlNodeType, String, String) CreateNode(XmlNodeType, String, String)

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

CreateNode(XmlNodeType, String, String, String) CreateNode(XmlNodeType, String, String, String) CreateNode(XmlNodeType, String, String, String) CreateNode(XmlNodeType, String, String, String)

建立具有指定之 XmlNodeTypePrefixNameNamespaceURIXmlNodeCreates a XmlNode with the specified XmlNodeType, Prefix, Name, and NamespaceURI.

CreateNode(String, String, String) CreateNode(String, String, String) CreateNode(String, String, String) 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);
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 String String 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 String String 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 String String String

新節點的命名空間 URI。The namespace URI of the new node.

傳回

新的 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
屬性attribute 屬性Attribute
cdatasectioncdatasection CDATACDATA
註解comment 註解Comment
文件document 文件Document
documentfragmentdocumentfragment DocumentFragmentDocumentFragment
documenttypedocumenttype DocumentTypeDocumentType
項目element 元素Element
entityreferenceentityreference EntityReferenceEntityReference
processinginstructionprocessinginstruction ProcessingInstructionProcessingInstruction
significantwhitespacesignificantwhitespace SignificantWhitespaceSignificantWhitespace
文字text TextText
whitespacewhitespace WhitespaceWhitespace

雖然這個方法會在檔的內容中建立新的物件, 但它不會自動將新的物件加入至檔樹狀結構。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) (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 屬性Attribute TextText 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 節點中才允許使用 Element 和 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) CreateNode(XmlNodeType, String, String) CreateNode(XmlNodeType, String, String) 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);
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 XmlNodeType XmlNodeType

新節點的 XmlNodeTypeThe XmlNodeType of the new node.

name
String String String 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 String String String

新節點的命名空間 URI。The namespace URI of the new node.

傳回

新的 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) (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 屬性Attribute TextText 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 節點中才允許使用 Element 和 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) CreateNode(XmlNodeType, String, String, String) CreateNode(XmlNodeType, String, String, String) 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);
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 XmlNodeType XmlNodeType

新節點的 XmlNodeTypeThe XmlNodeType of the new node.

prefix
String String String String

新節點的前置詞。The prefix of the new node.

name
String String String String

新節點的區域名稱。The local name of the new node.

namespaceURI
String String String String

新節點的命名空間 URI。The namespace URI of the new node.

傳回

新的 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) (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 屬性Attribute TextText 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 節點中才允許使用 Element 和 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).

適用於