ASP.NET,XML做一些推薦欄目。

 這個是用XML作爲數據庫。

用來做一些類似欄目推廣啊,反正就是小型的窗口推廣之類的。

上代碼

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <root> 
  3.   <group id="1"> 
  4.     <ad id="1" lnkurl="images/pto/0.jpg" imgurl="images/pto/0.jpg">我愛好C#</ad> 
  5.     <ad id="2" lnkurl="images/pto/1.jpg" imgurl="images/pto/1.jpg">My book2</ad> 
  6.     <ad id="3" lnkurl="images/pto/2.jpg" imgurl="images/pto/2.jpg">My book3</ad> 
  7.     <ad id="4" lnkurl="images/pto/3.jpg" imgurl="images/pto/3.jpg">My book4</ad> 
  8.   </group> 
  9.   <group id="2"> 
  10.     <ad id="1" lnkurl="images/pto/0.jpg" imgurl="images/pto/0.jpg">My book1</ad> 
  11.     <ad id="2" lnkurl="images/pto/1.jpg" imgurl="images/pto/1.jpg">My book2</ad> 
  12.   </group> 
  13. </root> 

以上就是XML文件

下面上ASPX文件內容

 

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="imagechange.aspx.cs" Inherits="imagechange" %> 
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  4.  
  5. <html xmlns="http://www.w3.org/1999/xhtml"> 
  6. <head runat="server"> 
  7.     <title></title> 
  8.     <style> 
  9.         .addbk 
  10.         { 
  11.             background-color:Gray; 
  12.             float:left; 
  13.             width:900px; 
  14.             } 
  15.         .addbk ul li 
  16.         { 
  17.             float:left; 
  18.             width:250px; 
  19.              
  20.             } 
  21.  
  22.             .alist{width:200px;} 
  23.             #a {text-decoration:none;} 
  24.     </style> 
  25. </head> 
  26. <body> 
  27.     <form id="form1" runat="server"> 
  28.     <div class="addbk"> 
  29.         <ul> 
  30.         <asp:Repeater ID="rptImageShow" runat="server"> 
  31.             <ItemTemplate> 
  32.             <li> 
  33.             <div class="alist"> 
  34.              <u><a href="<%#Eval("lnkurl") %>"><img src="<%#Eval("imgurl") %>" /></a></u> 
  35.              <i><a href=""<%#Eval("lnkurl") %>"><%#Eval("Description")%></a></i> 
  36.              </div> 
  37.             </li> 
  38.             </ItemTemplate> 
  39.         </asp:Repeater> 
  40.              
  41.             <!-- 
  42.             <li> 
  43.             <div class="alist"> 
  44.              <u><a href=""><img src="images/pto/0.jpg" /></a></u> 
  45.              <i><a href="">hhh</a></i> 
  46.              </div> 
  47.             </li> 
  48.              
  49.             <li> 
  50.             <div class="alist"> 
  51.              <u><a href=""><img src="images/pto/0.jpg" /></a></u> 
  52.              <i><a href="">hhh</a></i> 
  53.              </div> 
  54.             </li> 
  55.             --> 
  56.  
  57.  
  58.         </ul> 
  59.     </div> 
  60.     </form> 
  61. </body> 
  62. </html> 

以下是ASPX.CS文件

 

  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Web; 
  5. using System.Web.UI; 
  6. using System.Web.UI.WebControls; 
  7. using System.Xml; 
  8.  
  9. public partial class imagechange : System.Web.UI.Page 
  10.     protected void Page_Load(object sender, EventArgs e) 
  11.     { 
  12.         ImageShowHelper hp = new ImageShowHelper(); 
  13.         hp.dataRoot = Server.MapPath("~/app_data/adlqy.xml"); 
  14.         this.rptImageShow.DataSource = hp.GetImageListByGroupId(1); 
  15.         this.rptImageShow.DataBind(); 
  16.  
  17.  
  18.  
  19.          
  20.         //IList<ImageShow> l = hp.GetImageListByGroupId(1); 
  21.  
  22.  
  23.         //l[0].Description = "我愛好C#"; 
  24.         //hp.SaveXmlDocumentByGroupId(l, 1); 
  25.     } 
  26.  
  27.  
  28.     class ImageShowHelper  
  29.     { 
  30.  
  31.         IList<ImageShow> ilist; 
  32.         public string dataRoot = ""
  33.         public IList<ImageShow> GetImageListByGroupId(int groupid)  
  34.         { 
  35.             XmlDocument xmldoc = new XmlDocument(); 
  36.             xmldoc.Load(dataRoot); 
  37.             ImageShow model; 
  38.             XmlNode xnlist = xmldoc.SelectSingleNode("root"); 
  39.             ilist = new List<ImageShow>(); 
  40.             foreach (XmlNode xn in xnlist.ChildNodes)//Get group List 
  41.             { 
  42.                 if (xn.Attributes["id"].Value==groupid.ToString())//Get Group 
  43.                 { 
  44.                     XmlNodeList adxnlist = xn.SelectNodes("ad");//Get ad List 
  45.                     foreach (XmlNode xnImageObj in adxnlist)//Get ad 
  46.                     { 
  47.                         model = new ImageShow(); 
  48.                         model.Id = Convert.ToInt32(xnImageObj.Attributes["id"].Value); 
  49.                         model.Imgurl = xnImageObj.Attributes["imgurl"].Value; 
  50.                         model.Lnkurl = xnImageObj.Attributes["lnkurl"].Value; 
  51.                         model.Description = xnImageObj.InnerText; 
  52.                         ilist.Add(model); 
  53.                     } 
  54.                     break
  55.                 } 
  56.             } 
  57.             return ilist; 
  58.         } 
  59.  
  60.         public bool SaveXmlDocumentByGroupId(IList<ImageShow> showlist,int groupid)  
  61.         { 
  62.             bool isSave = false
  63.  
  64.             XmlDocument xmldoc = new XmlDocument(); 
  65.             xmldoc.Load(dataRoot); 
  66.             XmlNode xnlist = xmldoc.SelectSingleNode("root"); 
  67.  
  68.             foreach (XmlNode xn in xnlist.ChildNodes)//Get group List 
  69.             { 
  70.                 if (xn.Attributes["id"].Value == groupid.ToString())//Get Group 
  71.                 { 
  72.                     XmlNodeList adxnlist = xn.SelectNodes("ad");//Get ad List 
  73.  
  74.                     for (int i = 0; i < adxnlist.Count; i++) 
  75.                     { 
  76.                         XmlNode xnImageObject = adxnlist.Item(i); 
  77.                         xnImageObject.Attributes["imgurl"].Value=showlist[i].Imgurl; 
  78.                         xnImageObject.Attributes["lnkurl"].Value=showlist[i].Lnkurl; 
  79.                         xnImageObject.InnerText = showlist[i].Description; 
  80.                     } 
  81.                     break
  82.                 } 
  83.             } 
  84.             xmldoc.Save(dataRoot); 
  85.  
  86.  
  87.             return isSave; 
  88.         } 
  89.  
  90.  
  91.     } 
  92.      
  93.  
  94.     class ImageShow 
  95.     { 
  96.         int id; 
  97.  
  98.         public int Id 
  99.         { 
  100.             get { return id; } 
  101.             set { id = value; } 
  102.         } 
  103.         string lnkurl; 
  104.  
  105.         public string Lnkurl 
  106.         { 
  107.             get { return lnkurl; } 
  108.             set { lnkurl = value; } 
  109.         } 
  110.         string imgurl; 
  111.  
  112.         public string Imgurl 
  113.         { 
  114.             get { return imgurl; } 
  115.             set { imgurl = value; } 
  116.         } 
  117.         string description; 
  118.  
  119.         public string Description 
  120.         { 
  121.             get { return description; } 
  122.             set { description = value; } 
  123.         } 
  124.     } 

說一下思路:

因爲某些客戶,老是要在網頁的某個地方插入一些廣告。

所以就把廣告這個抽象了一下~

我主要是想分組,用來爲以後擴展做準備。

但是平常用也就一個組~所以就寫了兩個方法,根據組ID來存儲數據~

多組以後補上吧~

就是這樣了,源碼就不上傳了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章