XmlDocument.CreateEntityReference(String) 메서드

정의

지정된 이름을 가진 XmlEntityReference를 만듭니다.

public:
 virtual System::Xml::XmlEntityReference ^ CreateEntityReference(System::String ^ name);
public virtual System.Xml.XmlEntityReference CreateEntityReference (string name);
abstract member CreateEntityReference : string -> System.Xml.XmlEntityReference
override this.CreateEntityReference : string -> System.Xml.XmlEntityReference
Public Overridable Function CreateEntityReference (name As String) As XmlEntityReference

매개 변수

name
String

엔터티 참조의 이름입니다.

반환

XmlEntityReference

XmlEntityReference입니다.

예외

이름이 잘못되었습니다(예: '#'으로 시작하는 이름은 올바르지 않음).

예제

다음 예제에서는 두 엔터티 참조 노드를 만들고 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( "<!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 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;
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 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);
  }
}
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("<!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 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
End Class

설명

참조된 엔터티가 알려진 경우 노드의 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 및 따옴표도 허용되며 적절한 확장된 문자 값이 있는 자식 텍스트 노드가 있습니다.

이 메서드는 문서의 컨텍스트에서 새 개체를 만들지만 문서 트리에 새 개체를 자동으로 추가하지는 않습니다. 새 개체를 추가하려면 노드 삽입 메서드 중 하나를 명시적으로 호출해야 합니다.

W3C XML(Extensible Markup Language) 1.0 권장 사항에 따라 EntityReference 노드는 요소, 특성 및 EntityReference 노드 내에서만 허용됩니다.

적용 대상