This topic has not yet been rated - Rate this topic

XmlTextReader.XmlSpace Property

Gets the current xml:space scope.

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

public override XmlSpace XmlSpace { get; }
/** @property */
public XmlSpace get_XmlSpace ()

public override function get XmlSpace () : XmlSpace

Property Value

One of the XmlSpace values. If no xml:space scope exists, this property defaults to XmlSpace.None.
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.

The following example parses a file and returns significant white space if an xml:space='preserve' scope is found.

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

public class Sample{

  public static void Main(){

     XmlTextReader reader = new XmlTextReader("authors.xml");
     reader.WhitespaceHandling = WhitespaceHandling.None;

     // Parse the file.  Return white space only if an
     // xml:space='preserve' attribute is found.
     while (reader.Read()){
       switch (reader.NodeType){
         case XmlNodeType.Element:
           Console.Write("<{0}>", reader.Name);
           if (reader.XmlSpace==XmlSpace.Preserve)
             reader.WhitespaceHandling=WhitespaceHandling.Significant;
           break;
         case XmlNodeType.Text:
           Console.Write(reader.Value);
           break;
         case XmlNodeType.EndElement:
           Console.Write("</{0}>", reader.Name);
           break;
         case XmlNodeType.SignificantWhitespace:
           Console.Write(reader.Value);
           break;         
       }              
     }
  }
}

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

public class Sample
{
    public static void main(String[] args)
    {
        XmlTextReader reader = new XmlTextReader("authors.xml");
        reader.set_WhitespaceHandling(WhitespaceHandling.None);
        // Parse the file.  Return white space only if an
        // xml:space='preserve' attribute is found.
        while (reader.Read()) {
            switch (reader.get_NodeType()) {
                case XmlNodeType.Element:
                    Console.Write("<{0}>", reader.get_Name());
                    if (reader.get_XmlSpace().Equals(XmlSpace.Preserve)) {
                        reader.set_WhitespaceHandling(WhitespaceHandling.
                            Significant);
                    }
                    break;

                case XmlNodeType.Text:
                    Console.Write(reader.get_Value());
                    break;

                case XmlNodeType.EndElement:
                    Console.Write("</{0}>", reader.get_Name());
                    break;

                case XmlNodeType.SignificantWhitespace:
                    Console.Write(reader.get_Value());
                    break;
            }
        }
    } //main
} //Sample

The example uses the file, authors.xml, as input.

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
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ