XmlDocument.CreateNode メソッド

定義

XmlNode を作成します。

オーバーロード

CreateNode(String, String, String)

指定したノード型、Name、および NamespaceURI を使用して、XmlNode を作成します。

CreateNode(XmlNodeType, String, String)

指定した XmlNodeTypeName、および NamespaceURI を使用して、XmlNode を作成します。

CreateNode(XmlNodeType, String, String, String)

指定した XmlNodeTypePrefixName、および 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.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.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) 属性
cdatasection CDATA
コメント コメント
ドキュメント ドキュメント
documentfragment DocumentFragment
documenttype DocumentType
要素 要素
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 no no no no no no no no no
DocumentType はい no no no no no no no no
XmlDeclaration はい* no no no no no no no no
Element はい no no はい no no no no はい***
Attribute no no no はい**** no no no no no
Text no no no はい はい no no no はい
CDATA no no no はい no no no no はい***
Markup** はい no no はい no no no no no
EntityReference no no no はい はい no no no はい

* XmlDeclaration ノードは、Document ノードの最初の子である必要があります。

** マークアップには、ProcessingInstruction ノードと Comment ノードが含まれます。

Element ノードと CDATA ノードは、EntityReference ノードが Attribute ノードの子でない場合にのみ、EntityReference ノードで許可されます。

属性は Element ノードの子ではありません。 属性は、Element ノードに属する属性コレクション内に含まれます。

このメソッドは、ドキュメント オブジェクト モデル (DOM) のMicrosoft拡張機能です。

適用対象

CreateNode(XmlNodeType, String, String)

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

指定した XmlNodeTypeName、および 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.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.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 Extensible Markup Language (XML) 1.0 の推奨事項に従って、別の NodeType[column] 内で許可される NodeType[row] を示しています。

ドキュメント DocumentType XmlDeclaration 要素 属性 Text CDATA マークアップ EntityReference
Document no no no no no no no no no
DocumentType はい no no no no no no no no
XmlDeclaration はい* no no no no no no no no
Element はい no no はい no no no no はい***
Attribute no no no はい**** no no no no no
Text no no no はい はい no no no はい
CDATA no no no はい no no no no はい***
Markup** はい no no はい no no no no no
EntityReference no no no はい はい no no no はい

* XmlDeclaration ノードは、ドキュメント ノードの最初の子である必要があります。

** マークアップには、ProcessingInstruction ノードと Comment ノードが含まれます。

Element ノードと CDATA ノードは、EntityReference ノードが Attribute ノードの子でない場合にのみ EntityReference ノードで許可されます。

属性は Element ノードの子ではありません。 属性は、Element ノードに属する属性コレクション内に含まれます。

このメソッドは、ドキュメント オブジェクト モデル (DOM) のMicrosoft拡張機能です。

適用対象

CreateNode(XmlNodeType, String, String, String)

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

指定した XmlNodeTypePrefixName、および 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.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 Extensible Markup Language (XML) 1.0 の推奨事項に従って、別の NodeType[column] 内で許可される NodeType[row] を示しています。

ドキュメント DocumentType XmlDeclaration 要素 属性 Text CDATA マークアップ EntityReference
Document no no no no no no no no no
DocumentType はい no no no no no no no no
XmlDeclaration はい* no no no no no no no no
Element はい no no はい no no no no はい***
Attribute no no no はい**** no no no no no
Text no no no はい はい no no no はい
CDATA no no no はい no no no no はい***
Markup** はい no no はい no no no no no
EntityReference no no no はい はい no no no はい

* XmlDeclaration ノードは、ドキュメント ノードの最初の子である必要があります。

** マークアップには、ProcessingInstruction ノードと Comment ノードが含まれます。

Element ノードと CDATA ノードは、EntityReference ノードが Attribute ノードの子でない場合にのみ EntityReference ノードで許可されます。

属性は Element ノードの子ではありません。 属性は、Element ノードに属する属性コレクション内に含まれています。

このメソッドは、ドキュメント オブジェクト モデル (DOM) のMicrosoft拡張機能です。

適用対象