Export (0) Print
Expand All

XmlTextReader.WhitespaceHandling Property

Gets or sets a value that specifies how white space is handled.

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

public WhitespaceHandling WhitespaceHandling { get; set; }
/** @property */
public WhitespaceHandling get_WhitespaceHandling ()

/** @property */
public void set_WhitespaceHandling (WhitespaceHandling value)

public function get WhitespaceHandling () : WhitespaceHandling

public function set WhitespaceHandling (value : WhitespaceHandling)

Property Value

One of the WhitespaceHandling values. The default is WhitespaceHandling.All (returns Whitespace and SignificantWhitespace nodes).

Exception typeCondition

ArgumentOutOfRangeException

Invalid value specified.

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.

Because the XmlTextReader does not have DTD information available to it, SignificantWhitepsace nodes are only returned within an xml:space='preserve' scope.

The following example reads an XML fragment.

using System;
using System.IO;
using System.Xml;

public class Sample 
{
  public static void Main(){

    //Create the XML fragment to be parsed.
    string xmlFrag ="<book> " +
                    "  <title>Pride And Prejudice</title>" +
                    "  <genre>novel</genre>" +
                    "</book>"; 

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

    //Create the XmlParserContext.
    XmlParserContext context = new XmlParserContext(null, nsmgr, null, XmlSpace.Default);

    Console.WriteLine("Read the XML and ignore all white space...");
    ReadXML(context, xmlFrag, WhitespaceHandling.None);

    Console.WriteLine("\r\nRead the XML including white space nodes...");
    ReadXML(context, xmlFrag, WhitespaceHandling.All);

  }
  
  public static void ReadXML(XmlParserContext context, string xmlFrag, WhitespaceHandling ws){

    //Create the reader and specify the WhitespaceHandling setting.
    XmlTextReader reader = new XmlTextReader(xmlFrag, XmlNodeType.Element, context);
    reader.WhitespaceHandling = ws;

      //Parse the XML and display each of the nodes.
      while (reader.Read())
      {
         switch (reader.NodeType)
         {
           case XmlNodeType.Element:
             Console.WriteLine("{0}: <{1}>", reader.NodeType, reader.Name);
             break;
           case XmlNodeType.Text:
             Console.WriteLine("{0}: {1}", reader.NodeType, reader.Value);
             break;
           case XmlNodeType.EndElement:
             Console.WriteLine("{0}: </{1}>", reader.NodeType, reader.Name);
             break;
           case XmlNodeType.Whitespace:
             Console.WriteLine("{0}:", reader.NodeType);
             break;
           case XmlNodeType.SignificantWhitespace:
             Console.WriteLine("{0}:", reader.NodeType);
             break;
         }       
      }           
  
    //Close the reader.
    reader.Close();     
  
  }
} // End class


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 = "<book> " + "  <title>Pride And Prejudice</title>" 
		    + "  <genre>novel</genre>" + "</book>";

        //Create the XmlNamespaceManager.
        NameTable nt =  new NameTable();
        XmlNamespaceManager nsMgr =  new XmlNamespaceManager(nt);

        //Create the XmlParserContext.
        XmlParserContext context =  new XmlParserContext
            (null, nsMgr, null, XmlSpace.Default);
        Console.WriteLine("Read the XML and ignore all white space...");
        ReadXML(context, xmlFrag, WhitespaceHandling.None);
        Console.WriteLine("\r\nRead the XML including white space nodes...");
        ReadXML(context, xmlFrag, WhitespaceHandling.All);
    } //main

    public static void ReadXML(XmlParserContext context, String xmlFrag,
        WhitespaceHandling ws)
    {
        //Create the reader and specify the WhitespaceHandling setting.
        XmlTextReader reader = 
            new XmlTextReader(xmlFrag, XmlNodeType.Element, context);
        reader.set_WhitespaceHandling(ws);

        //Parse the XML and display each of the nodes.
        while(reader.Read()) {
            switch(reader.get_NodeType()) {
                case XmlNodeType.Element : 
                    Console.WriteLine("{0}: <{1}>", 
                        reader.get_NodeType(), reader.get_Name());
                    break;
                case XmlNodeType.Text : 
                    Console.WriteLine("{0}: {1}", 
                        reader.get_NodeType(), reader.get_Value());
                    break;
                case XmlNodeType.EndElement : 
                    Console.WriteLine("{0}: </{1}>", 
                        reader.get_NodeType(), reader.get_Name());
                    break;
                case XmlNodeType.Whitespace : 
                    Console.WriteLine("{0}:", reader.get_NodeType());
                    break;
                case XmlNodeType.SignificantWhitespace : 
                    Console.WriteLine("{0}:", reader.get_NodeType());
                    break;
            }
        }
        //Close the reader.
        reader.Close();
    } //ReadXML 
} // End class Sample

Windows 98, Windows 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 .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft