XmlTextReader.XmlSpace Property
Assembly: System.Xml (in system.xml.dll)
Note |
|---|
| 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.
Reference
XmlTextReader ClassXmlTextReader Members
System.Xml Namespace
Note