Solved

Read XML file using C#, limit and filter results

Posted on 2010-11-22
9
1,029 Views
Last Modified: 2012-05-10
Hi!

I am pretty new to .net
1. I want to read an XML file using C# and display data in web page.
2. I want to limit results to 10 records
3. I want to filter results using querystring

Here is what i have so far:


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
            <Columns>
                <asp:BoundField HeaderText="Title" DataField="innerText" />
            </Columns>
        </asp:GridView>
     </div>
    </form> 
</body>
</html>

Open in new window

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;

public partial class _Default : System.Web.UI.Page
{
    XmlDocument vidDocument = new XmlDocument();
    protected void Page_Load(object sender, EventArgs e)
    {
        vidDocument.Load(Server.MapPath("~/books.xml"));

        // this is how you get all the video nodes, perhaps to bind them or iterate over them
        XmlNodeList books = vidDocument.SelectNodes("/bookstore/book");

        // this would select all title elements
        XmlNodeList titles = vidDocument.GetElementsByTagName("title");

        // this binds the gridview to the title text to display
        GridView1.DataSource = titles;
        GridView1.DataBind();

    }

}

Open in new window

<?xml version="1.0" encoding="utf-8" ?>
<bookstore>
	<book genre="autobiography">
		<title>The Autobiography of Benjamin Franklin</title>
		<author>
			<first-name>Benjamin</first-name>
			<last-name>Franklin</last-name>
		</author>
		<price>8.99</price>
	</book>
	<book genre="novel">
		<title>The Confidence Man</title>
		<author>
			<first-name>Herman</first-name>
			<last-name>Melville</last-name>
		</author>
		<price>11.99</price>
	</book>
	<book genre="philosophy">
		<title>The Gorgias</title>
		<author>
			<name>Plato</name>
		</author>
		<price>9.99</price>
	</book>
</bookstore>

Open in new window

0
Comment
Question by:kuzmar
  • 4
  • 3
  • 2
9 Comments
 
LVL 9

Accepted Solution

by:
abhinayp86 earned 375 total points
ID: 34192500
The best and easy way is to use LINQ to XML.

http://www.hookedonlinq.com/LINQtoXML5MinuteOverview.ashx
0
 
LVL 9

Assisted Solution

by:abhinayp86
abhinayp86 earned 375 total points
ID: 34192524
0
 
LVL 21

Expert Comment

by:masterpass
ID: 34192579
you can directly bind the xml to the gridview

http://msdn.microsoft.com/en-us/library/13ftcwy9%28VS.80%29.aspx

or

you can just load the sml file to dataset and SET the dataset as datasource of the gridview

http://msdn.microsoft.com/en-us/library/fx29c3yd.aspx
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:kuzmar
ID: 34192651
Sound quite interesting. But a bit too much for me.
I am really just starting with ASP.NET and do not know much about it.

Maybe some real example?
0
 
LVL 9

Expert Comment

by:abhinayp86
ID: 34192668
yeah.. Here is the working example for ur xml



protected void Page_Load(object sender, EventArgs e)
    {

        // Create the query 
        var custs = from c in XElement.Load(Server.MapPath("books.xml")).Descendants("title")
                    select c;

        // Execute the query 

        GridView1.DataSource = custs;
        GridView1.DataBind();

    }

Open in new window

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">  
            <Columns>  
                <asp:BoundField HeaderText="Title" DataField="value" />  
            </Columns>  
        </asp:GridView>  

    </div>
    </form>
</body>
</html>

Open in new window

0
 
LVL 21

Expert Comment

by:masterpass
ID: 34192673
Well, if you take a look at the links, it's both implemented through examples
0
 

Author Comment

by:kuzmar
ID: 34192695
This is great... but how do I limit results and filter for example by "price"?
0
 
LVL 21

Assisted Solution

by:masterpass
masterpass earned 125 total points
ID: 34192732
that is some thing that you can do in gridview.

gridview.allowPaging = true; // for limiting
gridview.pagesize = 10;

gridview.allowSorting = true; // for sorting

and you will need to write the events for both paging and sorting

these are complete list of events http://msdn.microsoft.com/en-us/library/hf8xwy0t.aspx

look for pageindexchanged and sorting events and implement them .
0
 
LVL 9

Assisted Solution

by:abhinayp86
abhinayp86 earned 375 total points
ID: 34192856
To limit the results, u need to use .Take(10)
Eg:

 var custs = (from c in XElement.Load(Server.MapPath("books.xml")).Descendants("title")  
                    select c).Take(10);  

And to sort, u need to use orderby

0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question