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

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
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.