File: Default.aspx.cs using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Xml; public partial class HolmesQuote : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { MyQuote quotes = new MyQuote(Server.MapPath("./Data.xml")); Quotation quote = quotes.GetRandomQuote(); Response.Write("" + quote.Source + " (" + quote.Date + ")"); Response.Write("
" + quote.QuotationText + "
");
} } public class Quotation { private string qsource; private string date; private string quotation;
public Quotation(XmlNode quoteNode) { if ( (quoteNode.SelectSingleNode("source")) != null) qsource = quoteNode.SelectSingleNode("source").InnerText; if ( (quoteNode.Attributes.GetNamedItem("date")) != null) date = quoteNode.Attributes.GetNamedItem("date").Value; quotation = quoteNode.FirstChild.InnerText; } public string Source { get { return qsource; } set { qsource = value; } } public string Date { get { return date; } set { date = value; } } public string QuotationText { get { return quotation; } set { quotation = value; } } } public class MyQuote { private XmlDocument quoteDoc; private int quoteCount; public MyQuote(string fileName) { quoteDoc = new XmlDocument(); quoteDoc.Load(fileName); quoteCount = quoteDoc.DocumentElement.ChildNodes.Count; } public Quotation GetRandomQuote() { int i; Random x = new Random(); i = x.Next(quoteCount-1); return new Quotation( quoteDoc.DocumentElement.ChildNodes[i] ); } } File: Data.xml