Datasheet

using System.Data;
using System.Web;
/// <summary>
/// Summary description for ArticleData
/// </summary>
public class ArticleData
{
public void GetArticles(List<Article> articles, string year, string month)
{
DataSet dsArticles = new DataSet();
dsArticles.ReadXml(HttpContext.Current.Server.MapPath(“Articles.xml”));
DataView dvArticles = new DataView(dsArticles.Tables[“article”]);
dvArticles.RowFilter =
“year = ‘“ + year + “‘ “ +
“and month = ‘“ + month + “‘“;
Article currArticle = null;
IEnumerator articleRows = dvArticles.GetEnumerator();
while (articleRows.MoveNext())
{
DataRowView articleRow = (DataRowView)articleRows.Current;
currArticle = new Article(
(string)articleRow[“year”],
(string)articleRow[“month”],
(string)articleRow[“title”],
(string)articleRow[“content”]);
articles.Add(currArticle);
}
}
}
This class loads an XML file (see Listing 1-13) into a DataSet. It uses a DataView to filter the results based
on the year and month parameters that were passed in. The Article class is the business object that will
eventually be bound in the UI layer to a GridView. Of course, the UI layer doesn’t need to know that the
data came from an XML file or was processed via ADO.NET components. It can be easily changed later
to facilitate new requirements. Fortunately, the GridView is able to display business objects in a Generic
collection, which is why we pass the data back as List<Article> through the articles parameter.
Listing 1-13: Article data is represented via an XML file: Articles.xml
<?xml version=”1.0” encoding=”utf-8” ?>
<articles>
<article>
<year>2005</year>
<month>01</month>
<title>Title1</title>
<content>This is the text of Title1.</content>
</article>
<article>
(continued)
21
Hacks Revisited
04_597663 ch01.qxp 4/25/06 9:54 PM Page 21