XmlDocument.CreateEntityReference(String) Metoda

Definicja

Tworzy obiekt XmlEntityReference o określonej nazwie.

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

Parametry

name
String

Nazwa odwołania do jednostki.

Zwraca

XmlEntityReference

Nowy XmlEntityReferenceelement .

Wyjątki

Nazwa jest nieprawidłowa (na przykład nazwy rozpoczynające się od "#" są nieprawidłowe).

Przykłady

Poniższy przykład tworzy dwa węzły odwołania do jednostki i wstawia je do dokumentu 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

Uwagi

Jeśli jest znana przywoływanych jednostek, lista XmlEntityReference podrzędna węzła jest taka sama jak w przypadku odpowiedniego XmlEntity węzła.

Przestrzenie nazw używane w tekście zastępczym odwołania do jednostki są powiązane w momencie pierwszego ustawienia nadrzędnego węzła odwołania do jednostki (na przykład gdy węzeł odwołania do jednostki jest wstawiany do dokumentu). Na przykład biorąc pod uwagę następującą jednostkę:

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

Jeśli wywołasz CreateEntityReference("a") wywołanie, zostanie przywrócony jeden węzeł typu EntityReference bez elementów podrzędnych. Jeśli ten węzeł zostanie dołączony jako element podrzędny następującego węzła,

<item xmlns="urn:1"/>  

następnie w momencie wywoływania AppendChildmetody element nadrzędny nowo utworzonego węzła odwołania do jednostki jest ustawiony, a elementy podrzędne są rozwinięte w tym kontekście przestrzeni nazw. Węzeł b elementu podrzędnego będzie mieć identyfikator NamespaceURI równy urn:1. Węzły podrzędne odwołania do jednostki pozostają takie same, nawet jeśli przeniesiesz odwołanie do jednostki do miejsca w dokumencie, które ma inny domyślny kontekst przestrzeni nazw. Nie dzieje się tak w przypadku istniejących węzłów odwołania do jednostek podczas usuwania i wstawiania ich lub odwołań do jednostek sklonowanymi za pomocą CloneNodepolecenia . Dzieje się to tylko w przypadku nowo utworzonych odwołań do jednostek.

Jeśli odpowiednia jednostka nie jest zdefiniowana w dokumencie DocumentType podczas dodawania węzła odwołania do jednostki, ponieważ odwołanie do jednostki nie jest zdefiniowane, jego jedynym węzłem podrzędnym będzie pusty węzeł tekstowy.

Wbudowane jednostki amp, lt, gt, apos i cudzysłów są również dozwolone i będą mieć podrzędny węzeł tekstowy z odpowiednią rozwiniętą wartością znaku.

Mimo że ta metoda tworzy nowy obiekt w kontekście dokumentu, nie dodaje automatycznie nowego obiektu do drzewa dokumentów. Aby dodać nowy obiekt, należy jawnie wywołać jedną z metod wstawiania węzła.

Zgodnie z zaleceniem W3C Extensible Markup Language (XML) 1.0 węzły EntityReference są dozwolone tylko w węzłach Element, Attribute i EntityReference.

Dotyczy