Reading

The System.Xml namespace has several classes for making reading of XML files easy. These classes encapsulate the code necessary to parse these files and obtain data related to specific tags. Listing 18.2 shows how to read and parse pertinent data from the file that was written by the method in Listing 18.1.

Listing 18.2. Reading an XML Document
 /// <summary> /// read XML data from a file /// </summary> void ReadXML() { XmlTextReader xr = new XmlTextReader(fileName); string nodeName; while (xr.Read()) { nodeName = xr.Name; if (nodeName == "Talk") { talk.Add(xr.ReadString()); } } xr = new XmlTextReader(fileName); XmlDocument xd = new XmlDocument(); xd.PreserveWhitespace = true; xd.Load(xr); Console.WriteLine(xd.InnerXml); xr.Close(); } ...

Get C# Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.