Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > ASP.NET 4 General Discussion
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
ASP.NET 4 General Discussion For ASP.NET 4 discussions not relating to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 4 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old November 24th, 2011, 04:49 AM
Authorized User
Points: 303, Level: 6
Points: 303, Level: 6 Points: 303, Level: 6 Points: 303, Level: 6
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2005
Location: , , United Kingdom.
Posts: 71
Thanks: 23
Thanked 0 Times in 0 Posts
Default Page 345 - iterating through an xml file

Hi All, in Imar’s book on page 345 there is a section where it says something like the Timer control is good for updating the contents of an updatePanel, the source of which can be a xml file.

Does anyone know if this is something that we will be doing later in the book. If not can anyone tell me how to iterate through an xml file to update an updatePanel.

Many Thanks
Mark
Reply With Quote
  #2 (permalink)  
Old November 25th, 2011, 09:12 AM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hi there,

There are many, many ways to do this. To give you an idea of a possible solution, try this:

1. File | New Web Site

2. In App_Data create a file called Sample.xml and add the following data:

Code:
<?xml version="1.0" encoding="utf-8"?>
<articles>
  <article>
    <title>Article 1</title>
    <body>The body of the article goes here.</body>
  </article>
  <article>
    <title>Article 2</title>
    <body>The body of the article goes here.</body>
  </article>
  <article>
    <title>Article 3</title>
    <body>The body of the article goes here.</body>
  </article>
  <article>
    <title>Article 4</title>
    <body>The body of the article goes here.</body>
  </article>
  <article>
    <title>Article 5</title>
    <body>The body of the article goes here.</body>
  </article>
  <article>
    <title>Article 6</title>
    <body>The body of the article goes here.</body>
  </article>
</articles>
3. Create a new Web Form called Timer and add the following code:

Code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Timer.aspx.cs" Inherits="Timer" %>
<!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:ScriptManager ID="ScriptManager1" runat="server">
      </asp:ScriptManager>
      <asp:UpdatePanel ID="UpdatePanel1" runat="server">
      <ContentTemplate>
        <asp:Label ID="Label1" runat="server" Text="Label" />
        <asp:Timer ID="Timer1" runat="server" Interval="2000" ontick="Timer1_Tick">
        </asp:Timer>
      </ContentTemplate>
      </asp:UpdatePanel>
    </div>
    </form>
</body>
</html>
4. In the Code Behind, add the following Tick method:

Code:
protected void Timer1_Tick(object sender, EventArgs e)
{
  int counter = 0;
  object counterFromViewState = ViewState["Counter"];
  if (counterFromViewState != null)
  {
    counter = Convert.ToInt32(counterFromViewState);
  }
  XmlDocument doc = new XmlDocument();
  doc.Load(Server.MapPath("~/App_Data/sample.xml"));
  var nodes = doc.SelectNodes("//articles/article");
  if (nodes.Count <= counter)
  {
    counter = 0;
  }
  var nodeTitle = nodes[counter].SelectSingleNode("title").InnerText;
  counter++;
  ViewState["Counter"] = counter;
  Label1.Text = nodeTitle;
}
This code reads in the data from the XML file using an XmlDocument. It keeps track of the zero based index of each "article" in the XML file in ViewState. It then gets this item from the XML file, retrieves its title and assigns that to the Label. The next time the timer ticks, the value of the counter is one more than large time, giving you the next record. When the counter gets bigger than the number of available records, it gets set back to 0.

You should see this as an example, and not production ready code. You may want to add caching (to avoid using loading the XML file on each tick), and add error handling.

For more information: https://www.google.com/#sclient=psy-...=1050&bih=1234
https://www.google.com/#sclient=psy-...=1050&bih=1234

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
The Following User Says Thank You to Imar For This Useful Post:
markhh (November 25th, 2011)
  #3 (permalink)  
Old November 25th, 2011, 11:49 AM
Authorized User
Points: 303, Level: 6
Points: 303, Level: 6 Points: 303, Level: 6 Points: 303, Level: 6
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2005
Location: , , United Kingdom.
Posts: 71
Thanks: 23
Thanked 0 Times in 0 Posts
Default

Hi Imar and many thanks for your reply, I will look forward to having a go at it,

Regards
Mark
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Iterating through the LinkButtons in a page. nocofoolme ASP.NET 2.0 Basics 23 August 13th, 2008 04:01 PM
12345 to 12,345 >how to format? AlienLoi SQL Language 2 August 13th, 2007 05:43 AM
Can I define a new xsl page inside .XML file flyfish XSLT 0 August 29th, 2005 03:33 PM
Problem with For loop, iterating through XML Doc rj_mayer Classic ASP XML 2 April 6th, 2004 01:16 PM



All times are GMT -4. The time now is 09:32 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.