XmlTextReader.Normalization Proprietà

Definizione

Ottiene o imposta un valore che indica se normalizzare gli spazi vuoti e i valori degli attributi.

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

Valore della proprietà

true per normalizzare; in caso contrario, false. Il valore predefinito è false.

Eccezioni

Impostazione di questa proprietà quando il lettore viene chiuso (ReadState è ReadState.Closed).

Esempio

Nell'esempio seguente viene illustrato il comportamento del lettore con normalizzazione attivata e quindi disattivata.

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

Commenti

Note

È consigliabile creare XmlReader istanze usando il XmlReader.Create metodo per sfruttare le nuove funzionalità.

Questa proprietà può essere modificata in qualsiasi momento e ha effetto sull'operazione di lettura successiva.

Note

Se l'oggetto XmlTextReader viene usato per costruire un oggetto XmlValidatingReader, per normalizzare i valori di attributo, Normalization deve essere impostato su true.

Se Normalization è impostato su false, questo disabilita anche il controllo dell'intervallo di caratteri per le entità numeriche. Di conseguenza, le entità carattere, ad esempio &#0;, sono consentite.

Di seguito viene descritta la normalizzazione del valore dell'attributo:

  • Per un riferimento a un carattere, aggiungere il carattere a cui si fa riferimento al valore dell'attributo.

  • Per un riferimento a un'entità, elaborare in modo ricorsivo il testo sostitutivo dell'entità.

  • Per un carattere di spazio vuoto (#x20, #xD, #xA, #x9), aggiungere #x20 al valore normalizzato. Viene accodato solo un singolo #x20 per una sequenza "#xD#xA" che fa parte di un'entità analizzata esterna o del valore letterale di un'entità analizzata interna.

  • Elaborare altri caratteri aggiungendoli al valore normalizzato.

  • Se il valore dichiarato non è CDATA, eliminare eventuali caratteri di spazio iniziale e finale (#x20) e sostituire sequenze di caratteri di spazio (#x20) con un singolo spazio (#x20).

Esegue solo la normalizzazione dell'attributo XmlTextReader o CDATA. Non esegue la normalizzazione specifica di DTD a meno che non venga eseguito il wrapping all'interno di un oggetto XmlValidatingReader.

Per altre informazioni sulla normalizzazione, vedere la raccomandazione W3C XML 1.0.

Si applica a

Vedi anche