XmlDocument.CreateEntityReference メソッド
アセンブリ: System.Xml (system.xml.dll 内)

Dim instance As XmlDocument Dim name As String Dim returnValue As XmlEntityReference returnValue = instance.CreateEntityReference(name)
戻り値
新しい XmlEntityReference。


参照されたエンティティが既知の場合は、XmlEntityReference ノードの子リストは、対応する XmlEntity ノードの子リストと同じになります。
エンティティ参照の置換テキストで使用されている名前空間は、エンティティ参照ノードがドキュメントに挿入されたときなど、エンティティ参照ノードの親が最初に設定された時点でバインドされます。たとえば、次のエンティティを指定した場合、
CreateEntityReference("a") を呼び出すと、子のない EntityReference 型の単一ノードが返されます。このノードを次のノードの子として追加する場合は、
AppendChild を呼び出す時点で、新しく作成されたエンティティ参照ノードの親が設定され、子はこの名前空間コンテキストで展開されます。子要素ノード b の NamespaceURI は、urn:1 に等しくなります。既定の名前空間コンテキストが異なるドキュメント内の場所にエンティティ参照を移動しても、エンティティ参照の子ノードは同じままです。既存のエンティティ参照ノードを削除および挿入する場合、または CloneNode を使用してクローンを作成するエンティティ参照の場合は、子ノードは同じにはなりません。エンティティ参照の子ノードが同じままになるのは、新しく作成したエンティティ参照の場合だけです。
エンティティ参照ノードを追加したときに、対応するエンティティが DocumentType で定義されない場合は、エンティティ参照が定義されていないため、唯一の子ノードは空のテキスト ノードになります。
組み込みエンティティの amp、lt、gt、apos、quot も使用できます。これらのエンティティには、展開された適切な文字値を持つ子テキスト ノードがあります。
このメソッドは、ドキュメントのコンテキスト内で新しいオブジェクトを作成しますが、自動的には新しいオブジェクトをドキュメント ツリーに追加しません。新しいオブジェクトを追加するには、ノード挿入メソッドのいずれか 1 つを明示的に呼び出す必要があります。
W3C 勧告『Extensible Markup Language (XML) 1.0』(www.w3.org/TR/1998/REC-xml-19980210) に従って、EntityReference ノードは、Element、Attribute、EntityReference の各ノード内だけで使用できます。

2 つのエンティティ参照ノードを作成し、XML ドキュメントに挿入する例を次に示します。
Option Explicit Option Strict Imports System Imports System.IO Imports System.Xml Public Class Sample Public Shared Sub Main() 'Create the XmlDocument. Dim doc As New XmlDocument() doc.LoadXml("<!DOCTYPE book [<!ENTITY h 'hardcover'>]>" & _ "<book genre='novel' ISBN='1-861001-57-5'>" & _ "<title>Pride And Prejudice</title>" & _ "<misc/>" & _ "</book>") 'Create an entity reference node. The child count should be 0 'since the node has not been expanded. Dim entityref As XmlEntityReference = doc.CreateEntityReference("h") Console.WriteLine(entityref.ChildNodes.Count) 'After the the node has been added to the document, its parent node 'is set and the entity reference node is expanded. It now has a child 'node containing the entity replacement text. doc.DocumentElement.LastChild.AppendChild(entityref) Console.WriteLine(entityref.FirstChild.InnerText) 'Create and insert an undefined entity reference node. When the entity 'reference node is expanded, because the entity reference is undefined 'the child is an empty text node. Dim entityref2 As XmlEntityReference = doc.CreateEntityReference("p") doc.DocumentElement.LastChild.AppendChild(entityref2) Console.WriteLine(entityref2.FirstChild.InnerText) End Sub 'Main End Class 'Sample
using System; using System.IO; using System.Xml; public class Sample { public static void Main() { //Create the XmlDocument. XmlDocument doc = new XmlDocument(); doc.LoadXml("<!DOCTYPE book [<!ENTITY h 'hardcover'>]>" + "<book genre='novel' ISBN='1-861001-57-5'>" + "<title>Pride And Prejudice</title>" + "<misc/>" + "</book>"); //Create an entity reference node. The child count should be 0 //since the node has not been expanded. XmlEntityReference entityref = doc.CreateEntityReference("h"); Console.WriteLine(entityref.ChildNodes.Count ); //After the the node has been added to the document, its parent node //is set and the entity reference node is expanded. It now has a child //node containing the entity replacement text. doc.DocumentElement.LastChild.AppendChild(entityref); Console.WriteLine(entityref.FirstChild.InnerText); //Create and insert an undefined entity reference node. When the entity //reference node is expanded, because the entity reference is undefined //the child is an empty text node. XmlEntityReference entityref2 = doc.CreateEntityReference("p"); doc.DocumentElement.LastChild.AppendChild(entityref2); Console.WriteLine(entityref2.FirstChild.InnerText); } }
#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( "<!DOCTYPE book [<!ENTITY h 'hardcover'>]><book genre='novel' ISBN='1-861001-57-5'><title>Pride And Prejudice</title><misc/></book>" ); //Create an entity reference node. The child count should be 0 //since the node has not been expanded. XmlEntityReference^ entityref = doc->CreateEntityReference( "h" ); Console::WriteLine( entityref->ChildNodes->Count ); //After the the node has been added to the document, its parent node //is set and the entity reference node is expanded. It now has a child //node containing the entity replacement text. doc->DocumentElement->LastChild->AppendChild( entityref ); Console::WriteLine( entityref->FirstChild->InnerText ); //Create and insert an undefined entity reference node. When the entity //reference node is expanded, because the entity reference is undefined //the child is an empty text node. XmlEntityReference^ entityref2 = doc->CreateEntityReference( "p" ); doc->DocumentElement->LastChild->AppendChild( entityref2 ); Console::WriteLine( entityref2->FirstChild->InnerText ); }
import System.*; import System.IO.*; import System.Xml.*; public class Sample { public static void main(String[] args) { //Create the XmlDocument. XmlDocument doc = new XmlDocument(); doc.LoadXml("<!DOCTYPE book [<!ENTITY h 'hardcover'>]>" + "<book genre='novel' ISBN='1-861001-57-5'>" + "<title>Pride And Prejudice</title>" + "<misc/>" + "</book>"); //Create an entity reference node. The child count should be 0 //since the node has not been expanded. XmlEntityReference entityRef = doc.CreateEntityReference("h"); Console.WriteLine(entityRef.get_ChildNodes().get_Count()); //After the the node has been added to the document, its parent node //is set and the entity reference node is expanded. It now has a child //node containing the entity replacement text. doc.get_DocumentElement().get_LastChild().AppendChild(entityRef); Console.WriteLine(entityRef.get_FirstChild().get_InnerText()); //Create and insert an undefined entity reference node. When the entity //reference node is expanded, because the entity reference is undefined //the child is an empty text node. XmlEntityReference entityRef2 = doc.CreateEntityReference("p"); doc.get_DocumentElement().get_LastChild().AppendChild(entityRef2); Console.WriteLine(entityRef2.get_FirstChild().get_InnerText()); } //main } //Sample

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- XmlDocument.CreateEntityReference メソッドのページへのリンク