Export (0) Print
Expand All

XmlTextReader Constructor (TextReader)

Initializes a new instance of the XmlTextReader class with the specified TextReader.

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

public XmlTextReader (
	TextReader input
)
public XmlTextReader (
	TextReader input
)
public function XmlTextReader (
	input : TextReader
)

Parameters

input

The TextReader containing the XML data to read.

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.

It is assumed that the TextReader is already set to the correct encoding. This is used by clients that have already read some things from the stream in a multi-part MIME scenario.

The following example loads an XML string into the XmlTextReader object using the StringReader class.

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

public class Sample {

  public static void Main() {

    string xmlData = 
    @"<book>
       <title>Oberon's Legacy</title>
       <price>5.95</price>
      </book>";

    // Create the reader.
    XmlTextReader reader = new XmlTextReader(new StringReader(xmlData));
    reader.WhitespaceHandling = WhitespaceHandling.None;

    // Display each element node.
    while (reader.Read()){
       switch (reader.NodeType){
         case XmlNodeType.Element:
           Console.Write("<{0}>", reader.Name);
           break;
         case XmlNodeType.Text:
           Console.Write(reader.Value);
           break;
         case XmlNodeType.EndElement:
           Console.Write("</{0}>", reader.Name);
           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)
    {
        String xmlData = "<book>\n" + "       <title>Oberon's Legacy</title>\n" 
            + "       <price>5.95</price>\n" + "      </book>";
        // Create the reader.
        XmlTextReader reader = new XmlTextReader(new StringReader(xmlData));
        reader.set_WhitespaceHandling(WhitespaceHandling.None);
        // Display each element node.
        while (reader.Read()) {
            switch (reader.get_NodeType()) {
                case XmlNodeType.Element:
                    Console.Write("<{0}>", reader.get_Name());
                    break;

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

                case XmlNodeType.EndElement:
                    Console.Write("</{0}>", reader.get_Name());
                    break;
            }
        }
        // Close the reader.
        reader.Close();
    } //main
} //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:
© 2015 Microsoft