Team LiB
Previous Section Next Section

Reading XML

To read XML, you use XmlTextReader . You can read XML from a file, a stream, or a URL, as demonstrated in the following examples.

Code Example: Reading XML from a File
Start example
using System;
using System.IO;
using System.Xml;

namespace Client.Chapter_22___XML
{
          class Test
          {
                private const string doc = "Test.xml";
                static void Main(string[] args)
                {
                       XmlTextReader reader = null;
                       //Load the file with an XmlTextReader
                       reader = new XmlTextReader(doc);
                       //Read the file
                       while (reader.Read())
                       {
                             //TODO -
                       }
                       if (reader != null)
                          reader.Close();
                }
          }
}
End example
Code Example: Reading XML from a Stream
Start example

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

namespace Client.Chapter_22___XML
{
          class Test
          {
                 static void Main(string[] args)
                 {
                        StringReader stream;
                       XmlTextReader reader = null;
                       stream = new StringReader(" XML File Text");
                       //Load the XmlTextReader from the stream
                       reader = new XmlTextReader(stream);
                       while (reader.Read())
                       {
                             //TODO -
                       }
                       if (reader != null)
                              reader.Close();
                 }
          }
}
End example
Code Example: Reading XML from a URL
Start example

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

namespace Client.Chapter_22___XML
{
          class Test
          {
                static void Main(string[] args)
                {
                       string localURL = "http:\\Test\\Test.xml";
                       XmlTextReader myXmlURLreader = null;
                       myXmlURLreader = new XmlTextReader (localURL);
                       while (myXmlURLreader.Read())
                       {
                             //TODO -
                       }
                       if (myXmlURLreader != null)
                              myXmlURLreader.Close();
                }
          }
}
End example

Team LiB
Previous Section Next Section