技术开发 频道

滚动新闻图片的动态更新


【IT168技术文档】

  思路:
  1.当保存动态新闻图片的数据库表由更新完成时时,就再查询一次表,取前5条,将其保存到xml文件,页面数据直接从xml文件中读取。
  2.在页面的cs文件中 通过xmldocument和response.Write将内容输出到页面。
  具体的步骤代码如下:
  在后台动态新闻图片管理更新时,查询表生成xml文件
private void createXmldocument() { 创建xml文件#region 创建xml文件 string commandText = " select top 6 * from Pic"; conn = new SqlConnection(DbConnection.ConnectionString); SqlDataAdapter da = new SqlDataAdapter(commandText, conn); DataSet ds = new DataSet(); da.Fill(ds); XmlDocument xmldoc = new XmlDocument();//创建xml文档 XmlDeclaration desc = xmldoc.CreateXmlDeclaration("1.0", "utf-8", "yes");//创建声明 xmldoc.AppendChild(desc); XmlElement root = xmldoc.CreateElement("picture");//创建根元素 xmldoc.AppendChild(root); for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { XmlElement pic = xmldoc.CreateElement("pic"); XmlElement picUrl = xmldoc.CreateElement("picUrl"); picUrl.InnerText = ds.Tables[0].Rows[i][1].ToString(); pic.AppendChild(picUrl); XmlElement picPath = xmldoc.CreateElement("picPath"); picPath.InnerText = ds.Tables[0].Rows[i][2].ToString(); pic.AppendChild(picPath); XmlElement picText = xmldoc.CreateElement("picText"); picText.InnerText = ds.Tables[0].Rows[i][4].ToString(); pic.AppendChild(picText); xmldoc.DocumentElement.AppendChild(pic); } string xmlpath = Server.MapPath("~/picture.xml"); xmldoc.Save(xmlpath); #endregion }
0
相关文章