XmlTextReader.Normalization Property

Gets or sets a value indicating whether to normalize white space and attribute values.

Namespace: System.Xml
Assembly: System.Xml (in system.xml.dll)

public bool Normalization { get; set; }
/** @property */
public boolean get_Normalization ()

/** @property */
public void set_Normalization (boolean value)

public function get Normalization () : boolean

public function set Normalization (value : boolean)

Not applicable.

Property Value

true to normalize; otherwise, false. The default is false.

Exception typeCondition

InvalidOperationException

Setting this property when the reader is closed (ReadState is ReadState.Closed).

NoteNote:

In the Microsoft .NET Framework version 2.0 release, the recommended practice is to create XmlReader instances using the System.Xml.XmlReader.Create method. This allows you to take full advantage of the new features introduced in this release. For more information, see Creating XML Readers.

This property can be changed at any time and takes effect on the next read operation.

NoteNote:

If the XmlTextReader is used to construct an XmlValidatingReader, to normalize attribute values, Normalization must be set to true.

If Normalization is set to false, this also disables character range checking for numeric entities. As a result, character entities, such as �, are allowed.

The following describes attribute value normalization:

  • For a character reference, append the referenced character to the attribute value.

  • For an entity reference, recursively process the replacement text of the entity.

  • For a white space character (#x20, #xD, #xA, #x9), append #x20 to the normalized value. (Only a single #x20 is appended for a "#xD#xA" sequence that is part of an external parsed entity or the literal entity value of an internal parsed entity.)

  • Process other characters by appending them to the normalized value.

  • If the declared value is not CDATA, discard any leading and trailing space (#x20) characters and replace sequences of space (#x20) characters with a single space (#x20) character.

The XmlTextReader only performs attribute or CDATA normalization. It does not do DTD-specific normalization unless wrapped within an XmlValidatingReader.

Refer to the W3C XML 1.0 recommendation for further discussion on normalization.

The following example shows reader behavior with normalization turned on and then off.

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='&#01;'/>";                         

    // 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 &#00; to &#20; 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();     
  
  }
}

import System.*;
import System.IO.*;
import System.Xml.*;

public class Sample
{
    public static void main(String[] args)
    {
        // Create the XML fragment to be parsed.
        String xmlFrag = "<item attr1='  test A B C\n" + "        1 2 3'/>\n" 
            + "      <item attr2='&#01;'/>";
        // 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.set_Normalization(false);
        Console.WriteLine("Attribute value:{0}", reader.GetAttribute("attr1"));
        reader.set_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 &#00; to &#20; range.  If Normalization had
        // been set to true, character entities in this range throw an exception.
        reader.set_Normalization(false);
        reader.Read();
        reader.MoveToContent();
        Console.WriteLine("Attribute value:{0}", reader.GetAttribute("attr2"));
        // Close the reader.
        reader.Close();
    } //main 
} //Sample

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show: