次の方法で共有


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

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

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

パラメータ

  • nodeTypeString
    新しいノードの XmlNodeType の文字列バージョン。このパラメータは、次の表に示す一覧の値のいずれかである必要があります。
  • name
    新しいノードの限定名。名前にコロンが含まれている場合は、解析結果は Prefix コンポーネントと LocalName コンポーネントになります。
  • namespaceURI
    新しいノードの名前空間 URI。

戻り値

新しい XmlNode

例外

例外の種類 条件
ArgumentException 名前が指定されませんでした。 XmlNodeType には名前が必要です。または、 nodeTypeString が下に一覧表示した文字列ではありません。

解説

nodeTypeString パラメータでは大文字と小文字が区別され、次の表のいずれかの値である必要があります。

nodeTypeString XmlNodeType
attribute Attribute
cdatasection CDATA
comment Comment
document Document
documentfragment DocumentFragment
documenttype DocumentType
element Element
entityreference EntityReference
processinginstruction ProcessingInstruction
significantwhitespace SignificantWhitespace
text Text
whitespace Whitespace

このメソッドは、ドキュメントのコンテキスト内で新しいオブジェクトを作成しますが、自動的には新しいオブジェクトをドキュメント ツリーに追加しません。新しいオブジェクトを追加するには、ノード挿入メソッドのいずれか 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 = 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

[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 = 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);
   }
 }

[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 = doc->CreateNode(S"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 オーバーロードの一覧