This documentation is archived and is not being maintained.

XmlDocument.CreateEntityReference Method

Creates an XmlEntityReference with the specified name.

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

Parameters

name
The name of the entity reference.

Return Value

The new XmlEntityReference.

Exceptions

Exception Type Condition
ArgumentException The name is invalid (for example, names starting with'#' are invalid.)

Remarks

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

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"/>

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

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.

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.

According to the W3C Extensible Markup Language (XML) 1.0 recommendation (www.w3.org/TR/1998/REC-xml-19980210), EntityReference nodes are only allowed within Element, Attribute and EntityReference nodes.

Example

[Visual Basic, C#, C++] The following example creates two entity reference nodes and inserts them into an XML document.

[Visual Basic] 
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] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

XmlDocument Class | XmlDocument Members | System.Xml Namespace

Show: