Home > Code Samples > Read Any Size XML File in C#

Read Any Size XML File



Download DisplayDiggRSS.cs (Right Click->Save Target As...).

The code below shows how to use the XML functionality of the DataStreams framework to read an XML file of any size as a recordset using xpath expressions. The code downloads the current popular topics from the programming channel on Digg and writes the contents to the console. For small datasets, the entire recordset can be loaded into a DataTable by simply calling the ReadToEnd method instead. The DataTable can then be bound to a GridView and displayed for example. To bulk insert the contents into a sql server table instead, look at the code sample Bulk Insert XML File in C#.

C#
using System;
using System.IO;
using System.Net;
using System.Text;

using DataStreams.Xml;

namespace LargeXMLReader
{
	public class DisplayDiggRSS
	{
		static void Main(string[] args)
		{
			HttpWebRequest request = HttpWebRequest.Create(
				"http://feeds.digg.com/digg/topic/programming/popular.rss") as HttpWebRequest;

			using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
			using (Stream responseStream = response.GetResponseStream())
			using (XmlRecordReader reader = new XmlRecordReader(
				responseStream,
				Encoding.UTF8,
				"atom:feed/atom:entry",
				LoadMethod.Streaming))
			{
				reader.AddNamespace("digg", "http://digg.com/docs/diggrss/");
				reader.AddNamespace("media", "http://search.yahoo.com/mrss/");
				reader.AddNamespace("feedburner", "http://rssnamespace.org/feedburner/ext/1.0");
				reader.AddNamespace("atom", "http://www.w3.org/2005/Atom");

				reader.Columns.Add("atom:title", "title");
				reader.Columns.Add("atom:link/@href", "link");
				reader.Columns.Add("atom:content", "description");
				reader.Columns.Add("atom:updated", "pubDate");
				reader.Columns.Add("digg:diggCount", "diggCount");
				reader.Columns.Add("digg:category", "category");
				reader.Columns.Add("digg:commentCount", "commentCount");

				while (reader.ReadRecord())
				{
					Console.WriteLine("title: " + reader["title"]);
					Console.WriteLine("link: " + reader["link"]);
					Console.WriteLine("description: " + reader["description"]);
					Console.WriteLine("pubDate: " + reader["pubDate"]);
					Console.WriteLine("diggCount: " + reader["diggCount"]);
					Console.WriteLine("category: " + reader["category"]);
					Console.WriteLine("commentCount: " + reader["commentCount"]);
				}
			}

			Console.WriteLine("Done.");
			Console.ReadLine();
		}
	}
}