ObjectDataSource based on XML : ObjectDataSource « ADO.net Database « ASP.Net

Home
ASP.Net
1.ADO.net Database
2.Ajax
3.Asp Control
4.Collections
5.Components
6.Data Binding
7.Development
8.File Directory
9.HTML Control
10.Language Basics
11.Login Security
12.Mobile Control
13.Network
14.Page
15.Request
16.Response
17.Server
18.Session Cookie
19.Sitemap
20.Theme Style
21.User Control and Master Page
22.Validation by Control
23.Validation by Function
24.WebPart
25.WPF
26.XML
ASP.Net » ADO.net Database » ObjectDataSource 
ObjectDataSource based on XML

<%@ 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.cs

using 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>

 
Related examples in the same category
1.Binding to a LINQ to SQL Query
2.Binding to a Web Service
3.Using Parameters with the ObjectDataSource Control
4.Using Different Parameter Types
5.Passing Objects as Parameters
6.Filtering Data
7.Handling ObjectDataSource Control Events
8.Handling Method Errors
9.Handling the Object Creating Event
10.Concurrency and the ObjectDataSource Control, ConflictDetection: CompareAllValues / OverwriteChanges
11.Creating a Custom ObjectDataSource Control
12.Creating Custom Parameter Objects
13.Creating a Page Property Parameter
14.Define your own collection for ObjectDataSource
15.objectdatasource with control parameter
16.GridView with ObjectDataSource
17.ObjectDataSource with selectmethod, deletemethod, updatemethod, insertmethod
18.ObjectDataSource and backend database
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.