XmlDocument.CreateEntityReference(String) 方法

定义

创建具有指定名称的 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

实体引用的名称。The name of the entity reference.

返回

XmlEntityReference

新的 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 相同 XmlEntityIf 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:1The 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. 当你删除并插入现有实体引用节点时,或为你用克隆的实体引用删除它们时,不会发生这种情况 CloneNodeThis 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、中和 q 也是允许的,它们具有一个具有适当展开字符值的子文本节点。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) 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.

适用于