XmlDocument.CreateNode 方法

定義

建立 XmlNode

多載

CreateNode(String, String, String)

建立具有指定節點類型、NameNamespaceURIXmlNode

CreateNode(XmlNodeType, String, String)

建立具有指定的 XmlNodeTypeNameNamespaceURIXmlNode

CreateNode(XmlNodeType, String, String, String)

建立具有指定之 XmlNodeTypePrefixNameNamespaceURIXmlNode

CreateNode(String, String, String)

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

建立具有指定節點類型、NameNamespaceURIXmlNode

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

新節點的限定名稱。 如果名稱包含冒號,將會剖析為 PrefixLocalName 元件。

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
屬性 屬性
cdatasection CDATA
comment 註解
文件 文件
documentfragment DocumentFragment
documenttype DocumentType
element 項目
entityreference EntityReference
processinginstruction ProcessingInstruction
significantwhitespace SignificantWhitespace
text Text
whitespace 空白

雖然這個方法會在檔的內容中建立新的 物件,但不會自動將新物件加入檔樹狀結構。 若要新增物件,您必須明確呼叫其中一個節點插入方法。

下表顯示根據 W3C Extensible Markup Language (XML) 1.0 建議,在另一個 NodeType[column] 內允許哪些 NodeType[row] 。

文件 DocumentType XmlDeclaration 項目 屬性 Text CDATA 標記 EntityReference
Document
DocumentType
XmlDeclaration 是*
Element 是的***
Attribute 是的****
Text
CDATA 是的***
Markup**
EntityReference

* XmlDeclaration 節點必須是檔節點的第一個子節點。

** 標記包含 ProcessingInstruction 和 Comment 節點。

只有在 EntityReference 節點不是 Attribute 節點的子節點時,EntityReference 節點才允許元素和 CDATA 節點。

屬性不是元素節點的子系。 屬性包含在屬於 Element 節點的屬性集合內。

此方法是檔物件模型 (DOM) Microsoft延伸模組。

適用於

CreateNode(XmlNodeType, String, String)

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

建立具有指定的 XmlNodeTypeNameNamespaceURIXmlNode

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

新節點的限定名稱。 如果名稱包含冒號,將會剖析為 PrefixLocalName 元件。

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[column] 內允許哪些 NodeType[row]。

文件 DocumentType XmlDeclaration 項目 屬性 Text CDATA 標記 EntityReference
Document
DocumentType
XmlDeclaration 是*
Element 是的***
Attribute 是的****
Text
CDATA 是的***
Markup**
EntityReference

* XmlDeclaration 節點必須是檔節點的第一個子節點。

** 標記包含 ProcessingInstruction 和 Comment 節點。

只有在 EntityReference 節點不是 Attribute 節點的子節點時,EntityReference 節點才允許元素和 CDATA 節點。

屬性不是元素節點的子系。 屬性包含在屬於 Element 節點的屬性集合內。

此方法是檔物件模型 (DOM) Microsoft延伸模組。

適用於

CreateNode(XmlNodeType, String, String, String)

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

建立具有指定之 XmlNodeTypePrefixNameNamespaceURIXmlNode

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.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[column] 內允許哪些 NodeType[row]。

文件 DocumentType XmlDeclaration 項目 屬性 Text CDATA 標記 EntityReference
Document
DocumentType
XmlDeclaration 是*
Element 是的***
Attribute 是的****
Text
CDATA 是的***
Markup**
EntityReference

* XmlDeclaration 節點必須是 Document 節點的第一個子系。

** 標記包含 ProcessingInstruction 和 Comment 節點。

只有在 EntityReference 節點不是 Attribute 節點的子節點時,EntityReference 節點才允許元素和 CDATA 節點。

屬性不是元素節點的子系。 屬性包含在屬於元素節點的屬性集合內。

這個方法是檔物件模型 (DOM) Microsoft延伸模組。

適用於