System.Xml.XmlDocument.CreateEntityReference Method

Creates an System.Xml.XmlEntityReference with the specified name.

Syntax

public virtual XmlEntityReference CreateEntityReference (string name)

Parameters

name
The name of the entity reference.

Returns

The new XmlEntityReference.

Remarks

If the referenced entity is known, the child list of the XmlEntityReference node is made the same as that of the corresponding System.Xml.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:

Example

<!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,

Example

<item xmlns="urn:1"/>

then, at the time of calling XmlNode.AppendChild(XmlNode), 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 XmlDocument.CloneNode(bool). 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.

Requirements

Namespace: System.Xml
Assembly: System.Xml (in System.Xml.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0