XmlTextReader.Normalization Propiedad
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í.
Obtiene o establece un valor que indica si se deben normalizar los valores de atributo y de espacio en blanco.
public:
property bool Normalization { bool get(); void set(bool value); };
public bool Normalization { get; set; }
member this.Normalization : bool with get, set
Public Property Normalization As Boolean
Valor de propiedad
true si se deben normalizar; en caso contrario, false. De manera predeterminada, es false.
Excepciones
Esta propiedad se establece cuando el lector está cerrado (ReadState es ReadState.Closed).
Ejemplos
En el ejemplo siguiente se muestra el comportamiento del lector con la normalización activada y, a continuación, desactivada.
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
// Create the XML fragment to be parsed.
String^ xmlFrag = "<item attr1=' test A B C\n"
"1 2 3'/>\n"
"<item attr2=''/>\n";
// Create the XmlNamespaceManager.
XmlNamespaceManager^ nsmgr = gcnew XmlNamespaceManager( gcnew NameTable );
// Create the XmlParserContext.
XmlParserContext^ context = gcnew XmlParserContext( nullptr,nsmgr,nullptr,XmlSpace::Preserve );
// Create the reader.
XmlTextReader^ reader = gcnew XmlTextReader( xmlFrag,XmlNodeType::Element,context );
// Show attribute value normalization.
reader->Read();
reader->Normalization = false;
Console::WriteLine( "Attribute value: {0}", reader->GetAttribute( "attr1" ) );
reader->Normalization = true;
Console::WriteLine( "Attribute value: {0}", reader->GetAttribute( "attr1" ) );
// Set Normalization back to false. This allows the reader to accept
// character entities in the � to range. If Normalization had
// been set to true, character entities in this range throw an exception.
reader->Normalization = false;
reader->Read();
reader->MoveToContent();
Console::WriteLine( "Attribute value: {0}", reader->GetAttribute( "attr2" ) );
// Close the reader.
reader->Close();
}
using System;
using System.IO;
using System.Xml;
public class Sample{
public static void Main(){
// Create the XML fragment to be parsed.
string xmlFrag =
@"<item attr1=' test A B C
1 2 3'/>
<item attr2=''/>";
// Create the XmlNamespaceManager.
XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable());
// Create the XmlParserContext.
XmlParserContext context = new XmlParserContext(null, nsmgr, null, XmlSpace.Preserve);
// Create the reader.
XmlTextReader reader = new XmlTextReader(xmlFrag, XmlNodeType.Element, context);
// Show attribute value normalization.
reader.Read();
reader.Normalization = false;
Console.WriteLine("Attribute value:{0}", reader.GetAttribute("attr1"));
reader.Normalization = true;
Console.WriteLine("Attribute value:{0}", reader.GetAttribute("attr1"));
// Set Normalization back to false. This allows the reader to accept
// character entities in the � to range. If Normalization had
// been set to true, character entities in this range throw an exception.
reader.Normalization = false;
reader.Read();
reader.MoveToContent();
Console.WriteLine("Attribute value:{0}", reader.GetAttribute("attr2"));
// Close the reader.
reader.Close();
}
}
Imports System.IO
Imports System.Xml
public class Sample
public shared sub Main()
' Create the XML fragment to be parsed.
Dim xmlFrag as string = "<item attr1=' test A B C " + Chr(10) & _
" 1 2 3'/>" + Chr(10) & _
"<item attr2=''/>"
' Create the XmlNamespaceManager.
Dim nsmgr as XmlNamespaceManager = new XmlNamespaceManager(new NameTable())
' Create the XmlParserContext.
Dim context as XmlParserContext = new XmlParserContext(nothing, nsmgr, nothing, XmlSpace.Preserve)
' Create the reader.
Dim reader as XmlTextReader = new XmlTextReader(xmlFrag, XmlNodeType.Element, context)
' Show attribute value normalization.
reader.Read()
reader.Normalization = false
Console.WriteLine("Attribute value:{0}", reader.GetAttribute("attr1"))
reader.Normalization = true
Console.WriteLine("Attribute value:{0}", reader.GetAttribute("attr1"))
' Set Normalization back to false. This allows the reader to accept
' character entities in the � to range. If Normalization had
' been set to true, character entities in this range throw an exception.
reader.Normalization = false
reader.Read()
reader.MoveToContent()
Console.WriteLine("Attribute value:{0}", reader.GetAttribute("attr2"))
' Close the reader.
reader.Close()
end sub
end class
Comentarios
Nota
A partir de .NET Framework 2.0, se recomienda crear XmlReader instancias mediante el XmlReader.Create método para aprovechar las nuevas funcionalidades.
Esta propiedad se puede cambiar en cualquier momento y surte efecto en la siguiente operación de lectura.
Nota
XmlTextReader Si se usa para construir un XmlValidatingReaderobjeto , para normalizar los valores de atributo, Normalization debe establecerse en true.
Si Normalization se establece en false, también deshabilita la comprobación del intervalo de caracteres para las entidades numéricas. Como resultado, se permiten entidades de caracteres, como �, .
A continuación se describe la normalización del valor de atributo:
Para una referencia a carácter, adjunte al valor de atributo el carácter al que se hace referencia.
Para una referencia a entidad, procese de manera recursiva el texto de reemplazo de la entidad.
Para un carácter de espacio en blanco (#x20, #xD, #xA, #x9), anexe #x20 al valor normalizado. (Solo se anexa un solo #x20 para una secuencia "#xD#xA" que forma parte de una entidad analizada externa o el valor de entidad literal de una entidad analizada interna).
Para procesar otros caracteres, adjúntelos al valor normalizado.
Si el valor declarado no es CDATA, descarte los caracteres de espacio inicial y final (#x20) y reemplace secuencias de caracteres de espacio (#x20) por un solo carácter de espacio (#x20).
El XmlTextReader único realiza la normalización de atributos o CDATA. No realiza la normalización específica de DTD a menos que se encapsula dentro de .XmlValidatingReader
Consulte la recomendación W3C XML 1.0 para obtener más información sobre la normalización.