Show a random quote on asp.net web page

asp.net, c#

Solution

It could be something as simple as this with LINQ:

XElement xml = new XElement("quotes",
    new XElement("quote",
        new XElement("customer", "Customer #1"),
        new XElement("text", "Quote #1")),
    new XElement("quote",
        new XElement("customer", "Customer #2"),
        new XElement("text", "Quote #2")),
    new XElement("quote",
        new XElement("customer", "Customer #3"),
        new XElement("text", "Quote #3")),
    new XElement("quote",
        new XElement("customer", "Customer #4"),
        new XElement("text", "Quote #4")),
    new XElement("quote",
        new XElement("customer", "Customer #5"),
        new XElement("text", "Quote #5"))
);

//XElement xml = XElement.Load("filename"); // use file instead of above
var result = xml.Elements()
                .OrderBy(r => System.Guid.NewGuid())
                .Select(element => new { 
                        Customer = element.Element("customer").Value,
                        Quote = element.Element("text").Value
                    })
                .First();

Console.WriteLine("{0} : {1}", result.Customer, result.Quote);    

Your file would be structured like this:

<quotes>
  <quote>
    <customer>Customer #1</customer>
    <text>Quote #1</text>
  </quote>
  <quote>
    <customer>Customer #2</customer>
    <text>Quote #2</text>
  </quote>
  <quote>
    <customer>Customer #3</customer>
    <text>Quote #3</text>
  </quote>
  <quote>
    <customer>Customer #4</customer>
    <text>Quote #4</text>
  </quote>
  <quote>
    <customer>Customer #5</customer>
    <text>Quote #5</text>
  </quote>
</quotes>

You would load it using `XElement xml = XElement.Load("filename");`

With the `xml` variable above, the previous code is used the same way (commented out code).

The `Guid` works but you could also have a static Random variable defined in a class: `public static Random rand = new Random();` then change the code to:

int count = xml.Elements().Count();
var randomQuote = xml.Elements()
                     .OrderBy(i => rand.Next(0, count))
                     .Select(element => new { 
                        Customer = element.Element("customer").Value,
                        Quote = element.Element("text").Value
                      })
                     .First();

Console.WriteLine("{0} : {1}", result.Customer, result.Quote); 

Problem

I'm working on an ASP.NET (C#) web project that is using master pages. I'm looking for an easy way to display a random customer quote each time a page is loaded. Since this is a fairly simple web project I'd like to stay away from storing the quotes in a database. Currently there is no database connections required for the project so I'd like to keep it as simple as possible -- perhaps storing the quotes in an XML file them using an XmlTextReader to read the file? Any advice is appreciated. Thanks Edit: I will need to store and pull both a quote and a customer name for the quote.

Original source