CSharp Objectdatasource Based On Xml – ADO.Net Database

<%@ Page Language="C#" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Articles</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:GridView ID="GridView1" runat="server"
            AutoGenerateColumns="False" DataSourceID="ArticlesODS">
            <columns>
                <asp:BoundField DataField="Year"
                    HeaderText="Year" SortExpression="Year" />
                <asp:BoundField DataField="Month"
                    HeaderText="Month" SortExpression="Month" />
                <asp:BoundField DataField="Title"
                    HeaderText="Title" SortExpression="Title" />
                <asp:BoundField DataField="Content"
                    HeaderText="Content" SortExpression="Content" />
            </columns>
        </asp:GridView>
        <asp:ObjectDataSource ID="ArticlesODS" runat="server"
            SelectMethod="GetArticles" TypeName="Articles">
            <selectParameters>
                <asp:QueryStringParameter DefaultValue="2006"
                    Name="year" QueryStringField="year"
                    Type="String" />
                <asp:QueryStringParameter DefaultValue="01"
                    Name="month" QueryStringField="month"
                    Type="String" />
            </selectParameters>
        </asp:ObjectDataSource>
    </form>
</body>
</html>File: ArticleData.csusing System;
using System.Collections.Generic;class ArticleData
{
    public void GetArticles(List<article> articles, string year, string month)
    {
        DataSet dsArticles = new DataSet();
        dsArticles.ReadXml(HttpContext.Current.Server.MapPath("Data.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);
        }
    }
}class Article
{
    private string m_year;    public string Year
    {
        get { return m_year; }
        set { m_year = value; }
    }    private string m_month;    public string Month
    {
        get { return m_month; }
        set { m_month = value; }
    }
      private string m_title;    public string Title
    {
        get { return m_title; }
        set { m_title = value; }
    }    private string m_content;    public string Content
    {
        get { return m_content; }
        set { m_content = value; }
    }  public Article(string year, string month, string title, string content)
  {
        Year = year;
        Month = month;
        Title = title;
        Content = content;
  }
}public class Articles : List<article>
{
    public List<article> GetArticles(string year, string month)
    {
        ArticleData dal = new ArticleData();
        dal.GetArticles(this, year, month);
        return this;
    }
}File: Data.xml<?xml version="1.0" encoding="utf-8" ?>
<articles>  <article>
    <year>2005</year>
    <month>05</month>
    <title>Title6</title>
    <content>This is the text of Title6.</content>
  </article>
  <article>
    <year>2005</year>
    <month>06</month>
    <title>Title7</title>
    <content>This is the text of Title7.</content>
  </article></articles>

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.