XmlDocument.CreateEntityReference(String) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
创建具有指定名称的 XmlEntityReference。
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
实体引用的名称。
返回
新的 XmlEntityReference
。
例外
名称无效(例如,以“#”开头的名称无效。)
示例
以下示例创建两个实体引用节点,并将其插入 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
注解
如果引用的实体已知,则 XmlEntityReference
节点的子列表与相应 XmlEntity 节点的子列表相同。
实体引用的替换文本中使用的命名空间在实体引用节点的父级首次设置为 (时绑定,例如,将实体引用节点插入文档) 。 例如,给定以下实体:
<!ENTITY a "<b>test</b>">
如果调用 CreateEntityReference("a")
,则返回一个无子级的 EntityReference 类型的节点。 如果将此节点追加为以下节点的子节点,
<item xmlns="urn:1"/>
然后,在调用 AppendChild时,将设置新创建的实体引用节点的父节点,并且子级在此命名空间上下文中展开。 子元素节点 b
的 NamespaceURI 等于 urn:1
。 即使将实体引用移动到文档中具有不同默认命名空间上下文的位置,实体引用的子节点也保持不变。 删除和插入现有实体引用节点或克隆的 CloneNode实体引用时,不会发生这种情况。 它仅适用于新创建的实体引用。
如果在添加实体引用节点时 DocumentType 中未定义相应的实体,因为未定义实体引用,则其唯一的子节点将是空文本节点。
还允许内置实体 amp、lt、apos 和 quot,并且将具有具有适当扩展字符值的子文本节点。
尽管此方法在文档上下文中创建新对象,但它不会自动将新对象添加到文档树。 若要添加新对象,必须显式调用其中一个节点插入方法。
根据 W3C 可扩展标记语言 (XML) 1.0 建议,EntityReference 节点仅在元素、属性和 EntityReference 节点中允许。