Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > ASP.NET 4 General Discussion
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 24th, 2011, 04:49 AM
Authorized User
 
Join Date: Apr 2005
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
 
Old November 25th, 2011, 09:12 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 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!
The Following User Says Thank You to Imar For This Useful Post:
markhh (November 25th, 2011)
 
Old November 25th, 2011, 11:49 AM
Authorized User
 
Join Date: Apr 2005
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





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





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