次の方法で共有


XmlDocument.CreateEntityReference メソッド

指定した名前を使用して、 XmlEntityReference を作成します。

Public Overridable Function CreateEntityReference( _
   ByVal name As String _) As XmlEntityReference
[C#]
public virtual XmlEntityReference CreateEntityReference(stringname);
[C++]
public: virtual XmlEntityReference* CreateEntityReference(String* name);
[JScript]
public function CreateEntityReference(
   name : String) : XmlEntityReference;

パラメータ

  • name
    エンティティ参照の名前。

戻り値

新しい XmlEntityReference

例外

例外の種類 条件
ArgumentException 名前が無効です。たとえば、'#' で始まる名前は無効です。

解説

参照されたエンティティが既知の場合は、 XmlEntityReference ノードの子リストは、対応する XmlEntity ノードの子リストと同じになります。

エンティティ参照の置換テキストで使用されている名前空間は、エンティティ参照ノードがドキュメントに挿入されたときなど、エンティティ参照ノードの親が最初に設定された時点でバインドされます。たとえば、次のエンティティを指定した場合、

<!ENTITY a "<b>test</b>">

CreateEntityReference("a") を呼び出すと、子のない EntityReference 型の単一ノードが返されます。このノードを次のノードの子として追加する場合は、

<item xmlns="urn:1"/>

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 の各ノード内だけで使用できます。

使用例

[Visual Basic, C#, C++] 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

[C#] 
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);
    
  }
}

[C++] 
#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;

int main()
{
    //Create the XmlDocument.
    XmlDocument* doc = new XmlDocument();
    doc->LoadXml(S"<!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(S"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(S"p");
    doc->DocumentElement->LastChild->AppendChild(entityref2);
    Console::WriteLine(entityref2->FirstChild->InnerText);
}

[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 名前空間