XmlDocument.CreateEntityReference(String) XmlDocument.CreateEntityReference(String) XmlDocument.CreateEntityReference(String) XmlDocument.CreateEntityReference(String) Method

定義

建立具有指定名稱的 XmlEntityReferenceCreates an XmlEntityReference with the specified name.

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 String String String

實體參考的名稱。The name of the entity reference.

傳回

新的 XmlEntityReferenceThe new XmlEntityReference.

例外狀況

不正確的名稱 (例如以 '#' 開頭的名稱無效)。The name is invalid (for example, names starting with'#' are invalid.)

範例

下列範例會建立兩個實體參考節點, 並將它們插入 XML 檔中。The following example creates two entity reference nodes and inserts them into an XML document.

#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的節點相同。If the referenced entity is known, the child list of the XmlEntityReference node is made the same as that of the corresponding XmlEntity node.

實體參考的取代文字中使用的命名空間會在第一次設定實體參考節點的父系時系結 (例如, 當實體參考節點插入檔時)。The namespaces used in the replacement text for the entity reference are bound at the time the parent of the entity reference node is first set (for example, when the entity reference node is inserted into the document). 例如, 假設有下列實體:For example, given the following entity:

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

如果您呼叫CreateEntityReference("a") , 您會得到類型為 EntityReference 的單一節點, 而沒有子系。If you call CreateEntityReference("a") you get back a single node of type EntityReference with no children. 如果您附加此節點做為下列節點的子系,If you append this node as a child of the following node,

<item xmlns="urn:1"/>  

然後, 在呼叫AppendChild時, 會設定新建立之實體參考節點的父系, 並在此命名空間內容中展開子系。then, at the time of calling AppendChild, the parent of the newly created entity reference node is set and the children are expanded in this namespace context. 子項目節點b的 NamespaceURI 會urn:1等於。The child element node b will have NamespaceURI equal to urn:1. 即使您將實體參考移至檔中具有不同預設命名空間內容的位置, 實體參考的子節點仍會保持不變。The child nodes of the entity reference remain the same even if you move the entity reference to a place in the document that has a different default namespace context. 當您移除並插入, 或用於以CloneNode複製的實體參考時, 現有的實體參考節點不會發生這種情況。This does not happen for existing entity reference nodes when you remove and insert them or for entity references that you clone with CloneNode. 只有新建立的實體參考才會發生這種情況。It only happens for newly created entity references.

當加入實體參考節點時, 如果 DocumentType 中未定義對應的實體, 因為未定義實體參考, 所以它唯一的子節點會是空的文位元組點。If the corresponding entity is not defined in the DocumentType when the entity reference node is added, because the entity reference is not defined, its only child node will be an empty text node.

內建實體 amp、lt、gt、(和) 也是允許的, 而且會有具有適當擴充字元值的子文位元組點。The built-in entities amp, lt, gt, apos, and quot are also allowed, and they will have a child text node with the appropriate expanded character value.

雖然這個方法會在檔的內容中建立新的物件, 但它不會自動將新的物件加入至檔樹狀結構。Although this method creates the new object in the context of the document, it does not automatically add the new object to the document tree. 若要加入新的物件, 您必須明確地呼叫其中一個節點插入方法。To add the new object, you must explicitly call one of the node insert methods.

根據 W3C可延伸標記語言 (XML) (XML) 1.0 建議事項, 只有在元素、屬性和 entityreference 節點內才允許 EntityReference 節點。According to the W3C Extensible Markup Language (XML) 1.0 recommendation, EntityReference nodes are only allowed within Element, Attribute and EntityReference nodes.

適用於