次の方法で共有


XmlDocument.CreateNode メソッド (XmlNodeType, String, String, String)

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

Overloads Public Overridable Function CreateNode( _
   ByVal type As XmlNodeType, _   ByVal prefix As String, _   ByVal name As String, _   ByVal namespaceURI As String _) As XmlNode
[C#]
public virtual XmlNode CreateNode(XmlNodeTypetype,stringprefix,stringname,stringnamespaceURI);
[C++]
public: virtual XmlNode* CreateNode(XmlNodeTypetype,String* prefix,String* name,String* namespaceURI);
[JScript]
public function CreateNode(
   type : XmlNodeType,prefix : String,name : String,namespaceURI : String) : XmlNode;

パラメータ

  • type
    新しいノードの XmlNodeType
  • prefix
    新しいノードのプリフィックス。
  • name
    新しいノードのローカル名。
  • namespaceURI
    新しいノードの名前空間 URI。

戻り値

新しい XmlNode

例外

例外の種類 条件
ArgumentException 名前が指定されませんでした。 XmlNodeType には名前が必要です。

解説

このメソッドは、ドキュメントのコンテキスト内で新しいオブジェクトを作成しますが、自動的には新しいオブジェクトをドキュメント ツリーに追加しません。新しいオブジェクトを追加するには、ノード挿入メソッドのいずれか 1 つを明示的に呼び出す必要があります。

W3C 勧告『Extensible Markup Language (XML) 1.0』(www.w3.org/TR/1998/REC-xml-19980210) に従って、別の NodeType[列] 内で使用できる NodeType[行] を次の表に示します。

  Document DocumentType XmlDeclaration 要素 属性 Text CDATA Markup 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 ノードの最初の子にする必要があります。

** Markup には、ProcessingInstruction ノードおよび Comment ノードが含まれます。

*** Element ノードおよび CDATA ノードは、EntityReference ノードが Attribute ノードの子でない場合に、EntityReference ノードだけで使用できます。

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

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

使用例

[Visual Basic, C#, C++] 新しい要素をドキュメントに追加する例を次に示します。

 
Imports System
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
       newElem = doc.CreateNode(XmlNodeType.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

[C#] 
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;
       newElem = doc.CreateNode(XmlNodeType.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);
   }
 }

[C++] 
#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::Xml;
 
int main() 
{
       XmlDocument* doc = new XmlDocument();
       doc->LoadXml(S"<book>  <title>Oberon's Legacy</title>  <price>5.95</price></book>"); 
 
       // Create a new element node.
       XmlNode* newElem;
       newElem = doc->CreateNode(XmlNodeType::Element, S"pages", S"");  
       newElem->InnerText = S"290";
     
       Console::WriteLine(S"Add the new element to the document...");
       XmlElement* root = doc->DocumentElement;
       root->AppendChild(newElem);
     
       Console::WriteLine(S"Display the modified XML document...");
       Console::WriteLine(doc->OuterXml);
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

参照

XmlDocument クラス | XmlDocument メンバ | System.Xml 名前空間 | XmlDocument.CreateNode オーバーロードの一覧