4 Replies - 3660 Views - Last Post: 19 April 2009 - 08:53 PM Rate Topic: -----

#1 WebAppDeveloper  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 14-November 08

Dynamically populate a list of parameters from an xml file...

Posted 19 April 2009 - 06:19 PM

Greetings,

I have the following code (below) where I need to programmatically loop through data in an xml file and dynamically populate the necessary 3 parameters in the constructor "new AjaxControlToolkit.Slide(parameter1, parameter2, parameter3)" instead of repeating and hard-coding 4 instances of "new AjaxControlToolkit.Slide("apples.jpg", "Apples", "Some descriptions about apples.")", "new AjaxControlToolkit.Slide("oranges.jpg", "Oranges", "Some descriptions about oranges.")", etc. . Can someone please provide me with some sample code on how to do this?

Thanks very much in advance!

Here's the XML file:

<?xml version="1.0" encoding="utf-8" ?>
<SlideShows>
  <Slide ImageFile="apples.jpg">
	<ImageTitle>Apples</ImageTitle>
	<ImageDesc>Some descriptions about apples.</ImageDesc>
  </Slide>
  <Slide ImageFile="oranges.jpg">
	<ImageTitle>Oranges</ImageTitle>
	<ImageDesc>Some descriptions about oranges.</ImageDesc>
  </Slide>
  <Slide ImageFile="grapes.jpg">
	<ImageTitle>Grapes</ImageTitle>
	<ImageDesc>Some descriptions about grapes.</ImageDesc>
  </Slide>
  <Slide ImageFile="strawberry.jpg">
	<ImageTitle>Strawberries</ImageTitle>
	<ImageDesc>Some descriptions about strawberries.</ImageDesc>
  </Slide>
</SlideShows>


Here's the asp.net using C# code:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="sc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
	<title>Untitled Page</title>
	<link href="some_css_file.css" rel="stylesheet" type="text/css" />   
</head>
<body>
	<form id="form1" runat="server">
		
		<h2>Slide Show Extender Control</h2>
		
		<asp:ScriptManager ID="ScriptManager1" runat="server" />			   
		
		<script runat="Server" type="text/C#">
		[System.Web.Services.WebMethod]
		[System.Web.Script.Services.ScriptMethod]
		public static AjaxControlToolkit.Slide[] GetSlides()
		{
			return new AjaxControlToolkit.Slide[] { 
				new AjaxControlToolkit.Slide("apples.jpg", "Apples", "Some descriptions about apples."),
				new AjaxControlToolkit.Slide("oranges.jpg", "Oranges", "Some descriptions about oranges."),
				new AjaxControlToolkit.Slide("grapes.jpg", "Grapes", "Some descriptions about grapes."),
				new AjaxControlToolkit.Slide("strawberry.jpg", "Strawberries", "Some descriptions about strawberries.")
			};
		}
		</script>
		
		<div style="text-align:center">
		<table id="slideShow" border="0" cellpadding="0" cellspacing="0">
			<tr>
				<td>
					<asp:Image ID="imgSlide" runat="server" />			
			
					<sc1:SlideShowExtender ID="SlideShowExtender1"
					  AutoPlay="true" ImageDescriptionLabelID="lblImageDescription"
					   Loop="true" NextButtonID="btnNext" PlayButtonID="btnPlay" 
						PlayButtonText="Play" PreviousButtonID="btnPrevious" 
						 SlideShowServiceMethod="GetSlides" StopButtonText="Stop"
						  TargetControlID="imgSlide" PlayInterval="5000" runat="server">
					</sc1:SlideShowExtender>		  
				</td>
			</tr>
			<tr>
				<td id="slideShowButtons">
					<asp:Button ID="btnPrevious" runat="server" Text="Previous" />
					<asp:Button ID="btnPlay" runat="server" Text="Play" />
					<asp:Button ID="btnNext" runat="server" Text="Next" />
				</td>
			</tr>
			<tr>
				<td id="slideShowText">
					<asp:Label ID="lblImageDescription" runat="server" />
					<br /><br />
				</td>
			</tr>						
		</table>
		</div>
	</form>
</body>
</html>

This post has been edited by WebAppDeveloper: 19 April 2009 - 06:39 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Dynamically populate a list of parameters from an xml file...

#2 papuccino1  Icon User is offline

  • His name was Robert Paulson.
  • member icon

Reputation: 63
  • View blog
  • Posts: 1,121
  • Joined: 02-March 08

Re: Dynamically populate a list of parameters from an xml file...

Posted 19 April 2009 - 06:35 PM

Please wrap your code between code tags!!! It's not that hard dude! Help us help you!
Was This Post Helpful? 0
  • +
  • -

#3 WebAppDeveloper  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 14-November 08

Re: Dynamically populate a list of parameters from an xml file...

Posted 19 April 2009 - 06:39 PM

View Postpapuccino1, on 19 Apr, 2009 - 05:35 PM, said:

Please wrap your code between code tags!!! It's not that hard dude! Help us help you!


Done
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,515
  • Joined: 23-August 08

Re: Dynamically populate a list of parameters from an xml file...

Posted 19 April 2009 - 08:25 PM

XmlDocument doc = new XmlDocument();
doc.Load(@"..\..\test.xml");

XmlNodeList slides = doc.SelectNodes("SlideShows/Slide");
foreach (XmlNode slide in slides)
{
    Console.WriteLine("Slide file: {0}", slide.Attributes["ImageFile"].Value);
    Console.WriteLine("Slide title: {0}", slide.SelectSingleNode("ImageTitle").InnerText);
    Console.WriteLine("Slide desc: {0}", slide.SelectSingleNode("ImageDesc").InnerText);
}

Was This Post Helpful? 0
  • +
  • -

#5 WebAppDeveloper  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 14-November 08

Re: Dynamically populate a list of parameters from an xml file...

Posted 19 April 2009 - 08:53 PM

View PostWebAppDeveloper, on 19 Apr, 2009 - 05:19 PM, said:

Greetings,

I have the following code (below) where I need to programmatically loop through data in an xml file and dynamically populate the necessary 3 parameters in the constructor "new AjaxControlToolkit.Slide(parameter1, parameter2, parameter3)" instead of repeating and hard-coding 4 instances of "new AjaxControlToolkit.Slide("apples.jpg", "Apples", "Some descriptions about apples.")", "new AjaxControlToolkit.Slide("oranges.jpg", "Oranges", "Some descriptions about oranges.")", etc. . Can someone please provide me with some sample code on how to do this?

Thanks very much in advance!

Here's the XML file:

<?xml version="1.0" encoding="utf-8" ?>
<SlideShows>
  <Slide ImageFile="apples.jpg">
	<ImageTitle>Apples</ImageTitle>
	<ImageDesc>Some descriptions about apples.</ImageDesc>
  </Slide>
  <Slide ImageFile="oranges.jpg">
	<ImageTitle>Oranges</ImageTitle>
	<ImageDesc>Some descriptions about oranges.</ImageDesc>
  </Slide>
  <Slide ImageFile="grapes.jpg">
	<ImageTitle>Grapes</ImageTitle>
	<ImageDesc>Some descriptions about grapes.</ImageDesc>
  </Slide>
  <Slide ImageFile="strawberry.jpg">
	<ImageTitle>Strawberries</ImageTitle>
	<ImageDesc>Some descriptions about strawberries.</ImageDesc>
  </Slide>
</SlideShows>


Here's the asp.net using C# code:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="sc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
	<title>Untitled Page</title>
	<link href="some_css_file.css" rel="stylesheet" type="text/css" />   
</head>
<body>
	<form id="form1" runat="server">
		
		<h2>Slide Show Extender Control</h2>
		
		<asp:ScriptManager ID="ScriptManager1" runat="server" />			   
		
		<script runat="Server" type="text/C#">
		[System.Web.Services.WebMethod]
		[System.Web.Script.Services.ScriptMethod]
		public static AjaxControlToolkit.Slide[] GetSlides()
		{
			return new AjaxControlToolkit.Slide[] { 
				new AjaxControlToolkit.Slide("apples.jpg", "Apples", "Some descriptions about apples."),
				new AjaxControlToolkit.Slide("oranges.jpg", "Oranges", "Some descriptions about oranges."),
				new AjaxControlToolkit.Slide("grapes.jpg", "Grapes", "Some descriptions about grapes."),
				new AjaxControlToolkit.Slide("strawberry.jpg", "Strawberries", "Some descriptions about strawberries.")
			};
		}
		</script>
		
		<div style="text-align:center">
		<table id="slideShow" border="0" cellpadding="0" cellspacing="0">
			<tr>
				<td>
					<asp:Image ID="imgSlide" runat="server" />			
			
					<sc1:SlideShowExtender ID="SlideShowExtender1"
					  AutoPlay="true" ImageDescriptionLabelID="lblImageDescription"
					   Loop="true" NextButtonID="btnNext" PlayButtonID="btnPlay" 
						PlayButtonText="Play" PreviousButtonID="btnPrevious" 
						 SlideShowServiceMethod="GetSlides" StopButtonText="Stop"
						  TargetControlID="imgSlide" PlayInterval="5000" runat="server">
					</sc1:SlideShowExtender>		  
				</td>
			</tr>
			<tr>
				<td id="slideShowButtons">
					<asp:Button ID="btnPrevious" runat="server" Text="Previous" />
					<asp:Button ID="btnPlay" runat="server" Text="Play" />
					<asp:Button ID="btnNext" runat="server" Text="Next" />
				</td>
			</tr>
			<tr>
				<td id="slideShowText">
					<asp:Label ID="lblImageDescription" runat="server" />
					<br /><br />
				</td>
			</tr>						
		</table>
		</div>
	</form>
</body>
</html>


Ok, I got it. Here's the solution:

		<asp:ScriptManager ID="ScriptManager1" runat="server" />			   
		
		<script runat="Server" type="text/C#">
			[System.Web.Services.WebMethod]
			[System.Web.Script.Services.ScriptMethod]
			public static AjaxControlToolkit.Slide[] GetSlides()
			{
				HttpContext context = HttpContext.Current;
				string xmlPath = context.Server.MapPath("~/App_Data/slideshow.xml");
				XmlDocument doc = new XmlDocument();
				doc.Load(xmlPath);

				XmlNodeList imageFile = doc.SelectNodes("SlideShows/Slide");
				XmlNodeList imageTitle = doc.SelectNodes("SlideShows/Slide/ImageTitle");
				XmlNodeList imageDesc = doc.SelectNodes("SlideShows/Slide/ImageDesc");

				AjaxControlToolkit.Slide[] slides = new AjaxControlToolkit.Slide[imageFile.Count];

				for (int i = 0; i < imageFile.Count; i++)
				{
					slides[i] = new AjaxControlToolkit.Slide(imageFile[i].Attributes["ImageFile"].InnerText, imageTitle[i].InnerText, imageDesc[i].InnerText);
				}
				return slides;
			}
		</script>


Thanks for all the advice anyway.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1