This documentation is archived and is not being maintained.

XmlTextReader.Normalization Property

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

[Visual Basic]
Public Property Normalization As Boolean
[C#]
public bool Normalization {get; set;}
[C++]
public: __property bool get_Normalization();
public: __property void set_Normalization(bool);
[JScript]
public function get Normalization() : Boolean;
public function set Normalization(Boolean);

Property Value

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

Exceptions

Exception Type Condition
InvalidOperationException Setting this property when the reader is closed (ReadState is ReadState.Closed).

Remarks

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

Note   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.

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

Example

[Visual Basic, C#, C++] The following example shows reader behavior with normalization turned on and then off.

[Visual Basic] 
Imports System
Imports System.IO
Imports System.Xml
Imports Microsoft.VisualBasic

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

    ' 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 &#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()     
  
  end sub
end class

[C#] 
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();     
  
  }
}

[C++] 
#using <mscorlib.dll>
#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  = 
      S"<item attr1='  test A B C\n"
      S"1 2 3'/>\n"
      S"<item attr2='&#01;'/>\n";

   // Create the XmlNamespaceManager.
   XmlNamespaceManager* nsmgr = new XmlNamespaceManager(new NameTable());

   // Create the XmlParserContext.
   XmlParserContext* context = new XmlParserContext(0, nsmgr, 0, XmlSpace::Preserve);

   // Create the reader.
   XmlTextReader* reader = new XmlTextReader(xmlFrag, XmlNodeType::Element, context);

   // Show attribute value normalization.
   reader -> Read();
   reader -> Normalization = false;
   Console::WriteLine(S"Attribute value: {0}", reader -> GetAttribute(S"attr1"));
   reader -> Normalization = true;
   Console::WriteLine(S"Attribute value: {0}", reader -> GetAttribute(S"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(S"Attribute value: {0}", reader -> GetAttribute(S"attr2"));

   // Close the reader.
   reader -> Close();       
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

XmlTextReader Class | XmlTextReader Members | System.Xml Namespace

Show: