XmlNodeReader.ResolveEntity Método
Definición
Importante
Parte de la información hace referencia a la versión preliminar del producto, que puede haberse modificado sustancialmente antes de lanzar la versión definitiva. Microsoft no otorga ninguna garantía, explícita o implícita, con respecto a la información proporcionada aquí.
Resuelve la referencia a entidad de los nodos EntityReference
.
public:
override void ResolveEntity();
public override void ResolveEntity ();
override this.ResolveEntity : unit -> unit
Public Overrides Sub ResolveEntity ()
Excepciones
El lector no se encuentra en un nodo EntityReference
.
Ejemplos
En el ejemplo siguiente se usa ResolveEntity
para expandir una entidad general.
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
XmlNodeReader^ reader = nullptr;
try
{
//Create and load an XML document.
XmlDocument^ doc = gcnew XmlDocument;
doc->LoadXml( "<!DOCTYPE book [<!ENTITY h 'hardcover'>]>"
"<book>"
"<title>Pride And Prejudice</title>"
"<misc>&h;</misc>"
"</book>" );
//Create the reader.
reader = gcnew XmlNodeReader( doc );
reader->MoveToContent(); //Move to the root element.
reader->Read(); //Move to title start tag.
reader->Skip(); //Skip the title element.
//Read the misc start tag. The reader is now positioned on
//the entity reference node.
reader->ReadStartElement();
//You must call ResolveEntity to expand the entity reference.
//The entity replacement text is then parsed and returned as a child node.
Console::WriteLine( "Expand the entity..." );
reader->ResolveEntity();
Console::WriteLine( "The entity replacement text is returned as a text node." );
reader->Read();
Console::WriteLine( "NodeType: {0} Value: {1}", reader->NodeType, reader->Value );
Console::WriteLine( "An EndEntity node closes the entity reference scope." );
reader->Read();
Console::WriteLine( "NodeType: {0} Name: {1}", reader->NodeType, reader->Name );
}
finally
{
if ( reader != nullptr )
reader->Close();
}
}
using System;
using System.IO;
using System.Xml;
public class Sample
{
public static void Main()
{
XmlNodeReader reader = null;
try
{
//Create and load an XML document.
XmlDocument doc = new XmlDocument();
doc.LoadXml("<!DOCTYPE book [<!ENTITY h 'hardcover'>]>" +
"<book>" +
"<title>Pride And Prejudice</title>" +
"<misc>&h;</misc>" +
"</book>");
//Create the reader.
reader = new XmlNodeReader(doc);
reader.MoveToContent(); //Move to the root element.
reader.Read(); //Move to title start tag.
reader.Skip(); //Skip the title element.
//Read the misc start tag. The reader is now positioned on
//the entity reference node.
reader.ReadStartElement();
//You must call ResolveEntity to expand the entity reference.
//The entity replacement text is then parsed and returned as a child node.
Console.WriteLine("Expand the entity...");
reader.ResolveEntity();
Console.WriteLine("The entity replacement text is returned as a text node.");
reader.Read();
Console.WriteLine("NodeType: {0} Value: {1}", reader.NodeType ,reader.Value);
Console.WriteLine("An EndEntity node closes the entity reference scope.");
reader.Read();
Console.WriteLine("NodeType: {0} Name: {1}", reader.NodeType,reader.Name);
}
finally
{
if (reader != null)
reader.Close();
}
}
}
Option Explicit
Option Strict
Imports System.IO
Imports System.Xml
Public Class Sample
Public Shared Sub Main()
Dim reader As XmlNodeReader = Nothing
Try
'Create and load an XML document.
Dim doc As New XmlDocument()
doc.LoadXml("<!DOCTYPE book [<!ENTITY h 'hardcover'>]>" & _
"<book>" & _
"<title>Pride And Prejudice</title>" & _
"<misc>&h;</misc>" & _
"</book>")
'Create the reader.
reader = New XmlNodeReader(doc)
reader.MoveToContent() 'Move to the root element.
reader.Read() 'Move to title start tag.
reader.Skip() 'Skip the title element.
'Read the misc start tag. The reader is now positioned on
'the entity reference node.
reader.ReadStartElement()
'You must call ResolveEntity to expand the entity reference.
'The entity replacement text is then parsed and returned as a child node.
Console.WriteLine("Expand the entity...")
reader.ResolveEntity()
Console.WriteLine("The entity replacement text is returned as a text node.")
reader.Read()
Console.WriteLine("NodeType: {0} Value: {1}", reader.NodeType, reader.Value)
Console.WriteLine("An EndEntity node closes the entity reference scope.")
reader.Read()
Console.WriteLine("NodeType: {0} Name: {1}", reader.NodeType, reader.Name)
Finally
If Not (reader Is Nothing) Then
reader.Close()
End If
End Try
End Sub
End Class
Comentarios
Nota
En el .NET Framework 2.0, la práctica recomendada es crear XmlReader instancias mediante la XmlReaderSettings clase y el Create método . Esto le permite aprovechar al máximo todas las nuevas características introducidas en el .NET Framework. Para obtener más información, vea la sección Comentarios de la página de XmlReader referencia.
Si el lector se coloca en un EntityReference
nodo (XmlNodeType.EntityReference
), si Read se llama a después de llamar a este método, se analiza el texto de reemplazo de la entidad. Cuando finaliza el texto de reemplazo de entidad, se devuelve un EndEntity
nodo para cerrar el ámbito de referencia de entidad.
Nota
Después de llamar a este método, si la entidad forma parte de un valor de atributo, debe llamar ReadAttributeValue a para depurar paso a paso por instrucciones la entidad.