Saturday, August 18, 2007

Lucian's weblog : Retrieve data from Wikipedia using C#

This could be useful, since Wikipedia is now the source of all information in the world, along with Google & Facebook! 

To get other pages, you can simply use the direct link. The link is composed (as you’ve maybe already noticed) from followed by the name of the data you want to retrieve. So, if you want data about William Shakespeare, the direct link to the XML file will be

Knowing all this, it’s now simple to write a program to deal with the set of data provided by Wikipedia. The program looks something like this (and I’ll only give you the important part of the code, I’m sure you know where to put it):

private void button1_Click(object sender, EventArgs e)


System.Net.HttpWebRequest webRequest = (HttpWebRequest)System.Net.WebRequest.Create("");

webRequest.Credentials = System.Net.CredentialCache.DefaultCredentials;

webRequest.Accept = "text/xml";



System.Net.HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();

System.IO.Stream responseStream = webResponse.GetResponseStream();

System.Xml.XmlReader reader = new XmlTextReader(responseStream);

String NS = "";

XPathDocument doc = new XPathDocument(reader);



XPathNavigator myXPahtNavigator = doc.CreateNavigator();

XPathNodeIterator nodesText = myXPahtNavigator.SelectDescendants("text", NS, false);

while (nodesText.MoveNext())

textBox1.Text = nodesText.Current.InnerXml + " ";


catch (Exception ex)


textBox1.Text = ex.ToString();



Lucian's weblog : Retrieve data from Wikipedia using C#