个人开发历程知识库

关注C++/Java/C#技术, 致力于安防监控/移动应用/WEB方面开发
------------------------------------ 业精于勤,荒于嬉;行成于思,毁于随
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

使用xml+asp.net打造简单的站点导航&管理站点友情链接

Posted on 2006-04-24 22:29  peterzb  阅读(1690)  评论(0编辑  收藏  举报
效果图如下
站点的友情链接效果
xml文档绑定DataGrid进行操作
表格型的站点导航

存放站点导航信息的xml文档[links.xml]

<?xml version="1.0" encoding="gb2312"?>
<links>
  
<link>
    
<url>http://172.17.5.170/web/Article/ViewColumn.aspx?Colid=7</url>
    
<title>VB</title>
    
<target>_blank</target>
    
<name>VB</name>
  
</link>
  
<link>
    
<url>http://172.17.5.170/web/Article/ViewColumn.aspx?Colid=11</url>
    
<title>VBScript</title>
    
<target>_blank</target>
    
<name>VBScript</name>
  
</link>
  
<link>
    
<url>http://172.17.5.170/web/Article/ViewColumn.aspx?Colid=12</url>
    
<title>ASP</title>
    
<target>_blank</target>
    
<name>ASP</name>
  
</link>
  
<link>
    
<url>http://172.17.5.170/web/Article/ViewColumn.aspx?Colid=13</url>
    
<title>ASP.NET</title>
    
<target>_blank</target>
    
<name>ASP.NET</name>
  
</link>
  
<link>
    
<url>http://172.17.5.170/web/Article/ViewColumn.aspx?Colid=5</url>
    
<title>C#</title>
    
<target>_blank</target>
    
<name>C#</name>
  
</link>
  
<link>
    
<url>http://172.17.5.170/web/Article/ViewColumn.aspx?Colid=30</url>
    
<title>J#</title>
    
<target>_blank</target>
    
<name>J#</name>
  
</link>
  
<link>
    
<url>http://172.17.5.170/web/Article/ViewColumn.aspx?Colid=21</url>
    
<title>ADO.NET</title>
    
<target>_blank</target>
    
<name>ADO.NET</name>
  
</link>
  
<link>
    
<url>http://172.17.5.170/web/default.aspx</url>
    
<title>返回首页</title>
    
<target>_blank</target>
    
<name>返回首页</name>
  
</link>
  
<link>
    
<url>http://172.17.5.170/web/Article/ViewColumn.aspx?Colid=20</url>
    
<title>ADO</title>
    
<target>_blank</target>
    
<name>ADO</name>
  
</link>
  
<link>
    
<url>http://172.17.5.170/web/Article/ViewColumn.aspx?Colid=9</url>
    
<title>JAVA</title>
    
<target>_blank</target>
    
<name>JAVA</name>
  
</link>
  
<link>
    
<url>http://172.17.5.170/web/Article/ViewColumn.aspx?Colid=10</url>
    
<title>JavaScript</title>
    
<target>_self</target>
    
<name>JavaScript</name>
  
</link>
  
<link>
    
<url>http://172.17.5.170/web/Article/ViewColumn.aspx?Colid=14</url>
    
<title>HTML</title>
    
<target>_blank</target>
    
<name>HTML</name>
  
</link>
  
<link>
    
<url>http://172.17.5.170/web/Article/ViewColumn.aspx?Colid=15</url>
    
<title>XML</title>
    
<target>_blank</target>
    
<name>XML</name>
  
</link>
  
<link>
    
<url>http://172.17.5.170/web/Article/ViewColumn.aspx?Colid=16</url>
    
<title>CSS</title>
    
<target>_blank</target>
    
<name>CSS</name>
  
</link>
  
<link>
    
<url>http://172.17.5.170/web/Article/ViewColumn.aspx?Colid=3</url>
    
<title>C</title>
    
<target>_blank</target>
    
<name>C</name>
  
</link>
  
<link>
    
<url>http://172.17.5.170/web/Article/ViewColumn.aspx?Colid=4</url>
    
<title>C++</title>
    
<target>_blank</target>
    
<name>C++</name>
  
</link>
  
<link>
    
<url>http://172.17.5.170/web/Article/ViewColumn.aspx?Colid=6</url>
    
<title>Delphi</title>
    
<target>_blank</target>
    
<name>Delphi</name>
  
</link>
  
<link>
    
<url>http://172.17.5.170/web/Article/ViewColumn.aspx?Colid=30</url>
    
<title>PHP</title>
    
<target>_blank</target>
    
<name>PHP</name>
  
</link>
  
<link>
    
<url>http://172.17.5.170/web/Article/ViewColumn.aspx?Colid=5</url>
    
<title>.NET</title>
    
<target>_blank</target>
    
<name>.NET</name>
  
</link>
  
<link>
    
<url>http://172.17.5.170/web/resource/CSDN/CSDN论坛精华.html</url>
    
<title>CSDN论坛精华</title>
    
<target>_self</target>
    
<name>CSDN论坛精华</name>
  
</link>
  
<link>
    
<url>http://172.17.5.170/web/Article/ViewColumn.aspx?Colid=19</url>
    
<title>Database</title>
    
<target>_blank</target>
    
<name>Database</name>
  
</link>
  
<link>
    
<url>javascript:window.external.addFavorite('http://172.17.5.170/web/','Articles')</url>
    
<title>加入收藏</title>
    
<target>_self</target>
    
<name>加入收藏</name>
  
</link>
  
<link>
    
<url>http://172.17.5.170/web/Note/default.aspx?function=all</url>
    
<title>留言板</title>
    
<target>_blank</target>
    
<name>留言板</name>
  
</link>
  
<link>
    
<url>http://172.17.5.170/web/Download/default.aspx</url>
    
<title>下载</title>
    
<target>_blank</target>
    
<name>下载</name>
  
</link>
  
<link>
    
<url>http://172.17.5.170/web/Article/ViewColumn.aspx?Colid=22</url>
    
<title> PowerBuilder</title>
    
<target>_self</target>
    
<name>PowerBuilder</name>
  
</link>
  
<link>
    
<url>http://172.17.5.170/web/Article/column.htm</url>
    
<title>栏目查看</title>
    
<target>_blank</target>
    
<name>栏目查看</name>
  
</link>
  
<link>
    
<url>http://172.17.5.170/web/Article/ViewColumn.aspx?Colid=24</url>
    
<title>Novel</title>
    
<target>_blank</target>
    
<name>Novel</name>
  
</link>
  
<link>
    
<url>http://172.17.5.170/web/Article/searchArticle.aspx</url>
    
<title>文章搜索</title>
    
<target>_blank</target>
    
<name>文章搜索</name>
  
</link>
  
<link>
    
<url>http://172.17.5.170/web/blog/course.htm</url>
    
<title>个人Blog</title>
    
<target>_blank</target>
    
<name>个人Blog</name>
  
</link>
  
<link>
    
<url>http://172.17.5.170/web/contract.aspx</url>
    
<title>联系我们</title>
    
<target>_blank</target>
    
<name>联系我们</name>
  
</link>
  
<link>
    
<url>http://172.17.5.170/web/Article/default.aspx</url>
    
<title>文章系统</title>
    
<target>_blank</target>
    
<name>文章系统</name>
  
</link>
  
<link>
    
<url>http://172.17.5.170/web/Article/ViewColumn.aspx?Colid=35</url>
    
<title>Linux系统</title>
    
<target>_blank</target>
    
<name>Linux</name>
  
</link>
</links>
程序相关源代码[Links.cs] 该类可以用管理站点的友情链接与站点导航
using System;
using System.Data;
using System.Xml;
using Web.Common;

namespace Web
{
    
/// <summary>
    
/// Links 的摘要说明。
    
/// </summary>

    public class Links
    
{
        
/// <summary>
        
/// xml文档路径
        
/// </summary>

        private string fileName;
        
/// <summary>
        
/// 友情链接的url地址
        
/// </summary>

        private string url;
        
/// <summary>
        
/// 友情链接的title
        
/// </summary>

        private string title;
        
/// <summary>
        
/// 友情链接的target
        
/// </summary>

        private string target;
        
/// <summary>
        
/// 友情链接的名称
        
/// </summary>

        private string name;
        
/// <summary>
        
/// 页眉站点导航一行显示的超链接数目
        
/// </summary>

        private int columns=8;//Web.Common.WebConfig.Columns
        /// <summary>
        
/// 出错信息,操作反馈信息
        
/// </summary>

        private string info;

        
/// <summary>
        
/// 默认构造函数
        
/// </summary>

        public Links()
        
{
            setFileName(
"/web/links.xml");
        }


        
/// <summary>
        
/// 指定xml文档路径的构造函数
        
/// </summary>
        
/// <param name="xmlPath">xml文档路径</param>

        public Links(string xmlPath)
        
{
            setFileName(xmlPath);
        }


        
/// <summary>
        
/// xml文档路径,可以提供绝对路径,也可以提供相对路径,URL也可以
        
/// </summary>

        public string FileName
        
{
            
set
            
{
                setFileName(value);
            }

            
get return fileName; }
        }


        
/// <summary>
        
/// 友情链接的url地址
        
/// </summary>

        public string URL
        
{
            
set { url=value;  }
            
get return url; }
        }


        
/// <summary>
        
/// 友情链接的title
        
/// </summary>

        public string Title
        
{
            
set { title=value;  }
            
get return title; }
        }


        
/// <summary>
        
/// 友情链接的target
        
/// </summary>

        public string Target
        
{
            
set { target=value;  }
            
get return target; }
        }


        
/// <summary>
        
/// 友情链接的名称
        
/// </summary>

        public string Name
        
{
            
set { name=value;  }
            
get return name; }
        }


        
/// <summary>
        
/// 页眉站点导航一行显示的超链接数目
        
/// </summary>

        public int Columns
        
{
            
set { columns=value;  }
            
get return columns; }
        }


        
/// <summary>
        
/// 属性Info是一个[readonly]属性,表示对象Links进行相关操作
        
/// 后,程序给出的相关信息,如:无法完成某项操作,生成的出错信
        
/// 息;或者是成功执行了某项操作的提示信息
        
/// </summary>

        public string Info
        
{
            
get
            
{
                
return info;
            }

        }


        
/// <summary>
        
/// 设置xml文档路径,有可能需要转换
        
/// </summary>
        
/// <param name="path">文档路径</param>

        private void setFileName(string path)
        
{
            
if(path!=null&&path!="")
            
{
                
if(path.IndexOf(":")>0||path.IndexOf("\\")>0)//WinForm
                {
                    fileName
=path;
                }

                
//WebForm
                else
                
{
                    fileName
=System.Web.HttpContext.Current.Server.MapPath(path);
                }

            }

        }


        
/// <summary>
        
/// 查询,获取指定友情链接名称的友情链接信息
        
/// </summary>
        
/// <param name="name">友情链接名称</param>
        
/// <returns>操作是否成功,成功则得到该友情链接的信息</returns>

        public bool Query(string name)
        
{
            
try
            
{
                XmlDocument xmlDoc
=new XmlDocument();
                xmlDoc.Load(fileName);

                XmlNode root
=xmlDoc.SelectSingleNode("links");//查找<links>
                XmlElement element=(XmlElement)root.SelectSingleNode("//link[./name='"+Format.FormatXml(name)+"']") ;
                
if(element!=null)
                
{   
                    url
=element.ChildNodes[0].InnerText;
                    title
=element.ChildNodes[1].InnerText;
                    target
=element.ChildNodes[2].InnerText;
                    
this.name=element.ChildNodes[3].InnerText;
                    info
="找到名称为'"+name+"'的友情链接!";
                    
return true;
                }

                
else
                
{
                    info
="找不到名称为'"+name+"'的友情链接!";
                    
return false;
                }

            }

            
catch(Exception e)
            
{
                info
=e.Message;
                
return false;
            }

        }


        
/// <summary>
        
/// 添加一个link元素,该友情链接信息由属性设置
        
/// </summary>
        
/// <returns>操作是否成功</returns>

        public bool Add()
        
{
            
return add(url,title,target,name);
        }


        
/// <summary>
        
/// 添加一个link元素
        
/// </summary>
        
/// <param name="url">URL地址</param>
        
/// <param name="title">超链接标题</param>
        
/// <param name="target">超链接的target</param>
        
/// <param name="name">超链接名称</param>
        
/// <returns>操作是否成功</returns>

        public bool Add(string url,string title,string target,string name)
        
{
            
return add(url,title,target,name);
        }


        
/// <summary>
        
/// 添加一个link元素
        
/// </summary>
        
/// <param name="url">URL地址</param>
        
/// <param name="title">超链接标题</param>
        
/// <param name="target">超链接的target</param>
        
/// <param name="name">超链接名称</param>
        
/// <returns>操作是否成功</returns>

        private bool add(string url,string title,string target,string name)
        
{
            
try
            
{
                XmlDocument xmlDoc
=new XmlDocument();
                xmlDoc.Load(fileName);

                XmlNode root
=xmlDoc.SelectSingleNode("links");//查找<links>
                XmlElement addElem=(XmlElement)root.SelectSingleNode("//link[./name='"+Format.FormatXml(name)+"']") ;
                
if(addElem!=null)
                
{                    
                    info
="该元素'"+name+"'已经存在了!";
                    
return false;
                }

                
else
                
{
                    XmlElement xe1
=xmlDoc.CreateElement("link");//创建一个<link>节点
                    
//xe1.SetAttribute("genre","李赞红");//设置该节点genre属性
                    
//xe1.SetAttribute("ISBN","2-3631-4");//设置该节点ISBN属性
 
                    XmlElement urlElement
=xmlDoc.CreateElement("url");
                    urlElement.InnerText
=url;
                    xe1.AppendChild(urlElement);
                    XmlElement xesub1
=xmlDoc.CreateElement("title");
                    xesub1.InnerText
=title;//设置文本节点
                    xe1.AppendChild(xesub1);//添加到<link>节点中
                    XmlElement xesub2=xmlDoc.CreateElement("target");
                    xesub2.InnerText
=target;
                    xe1.AppendChild(xesub2);
                    XmlElement xesub3
=xmlDoc.CreateElement("name");
                    xesub3.InnerText
=name;
                    xe1.AppendChild(xesub3);
 
                    root.AppendChild(xe1);
//添加到<links>节点中
                    xmlDoc.Save(fileName);
                    info
="成功添加友情链接'"+name+"'";
                    
return true;
                }

            }

            
catch(Exception e)
            
{
                info
=e.Message;
                
return false;
            }

        }

        
        
/// <summary>
        
/// 添加一个link元素
        
/// </summary>
        
/// <param name="values">元素值集合,依次为[url,title,target,name]</param>
        
/// <returns>操作是否成功</returns>

        public bool Add(string[] values)
        
{
            
try
            
{
                XmlDocument xmlDoc
=new XmlDocument();
                xmlDoc.Load(fileName);
                XmlNode root
=xmlDoc.SelectSingleNode("links");//查找<links>
                XmlElement xe1=xmlDoc.CreateElement("link");//创建一个<link>节点
                
                
string[] elements={"url","title","target","name"};
                
for(int i=0;i<elements.Length;i++)
                
{
                    XmlElement element
=xmlDoc.CreateElement(elements[i]);
                    element.InnerText
=values[i];//设置文本节点
                    xe1.AppendChild(element);//添加到<link>节点中
                }

 
                root.AppendChild(xe1);
//添加到<links>节点中
                xmlDoc.Save(fileName);
                info
="成功添加友情链接'"+values[0]+"'";
                
return true;
            }

            
catch(Exception e)
            
{
                info
=e.Message;
                
return false;
            }

        }


        
/// <summary>
        
/// 删除一个指定超链接名称的友情链接元素
        
/// </summary>
        
/// <param name="name">超链接名称</param>
        
/// <returns>操作是否成功</returns>

        public bool remove(string name)
        
{
            
try
            
{
                XmlDocument doc 
= new XmlDocument(); 
                doc.Load(fileName);  
                XmlNode node
=doc.SelectSingleNode("//links");   
                
if(node!=null)
                
{    
                    node.RemoveChild(node.SelectSingleNode(
"//link[name='"+Format.FormatXml(name)+"']")); 
                    doc.Save(fileName);
                }

                info
="成功删除友情链接'"+name+"'";
                
return true;
            }

            
catch(Exception e)
            
{
                info
=e.Message;
                
return false;
            }

        }


        
/// <summary>
        
/// 删除一个指定超链接名称的友情链接元素
        
/// </summary>
        
/// <param name="name">超链接名称</param>
        
/// <returns>操作是否成功</returns>

        public bool removeElement(string name)
        
{
            
try
            
{
                XmlDocument xmlDoc
=new XmlDocument();   
                xmlDoc.Load(fileName);   
                XmlNode root
=xmlDoc.SelectSingleNode("links");
                XmlNodeList xnl
=xmlDoc.SelectSingleNode("links").ChildNodes;   
                
for(int i=0;i<xnl.Count;i++)
                
{
                    XmlElement xe
=(XmlElement)xnl.Item(i);   
                    
if(((XmlElement)(xe.LastChild)).InnerText==name)   
                    
{   
                        root.RemoveChild(xe);
                        
break;
                    }
   
                }

                xmlDoc.Save(fileName);
                info
="成功删除友情链接'"+name+"'";
                
return true;
            }

            
catch(Exception e)
            
{
                info
=e.Message;
                
return false;
            }

        }


        
/// <summary>
        
/// 更新指定名称的友情链接元素
        
/// </summary>
        
/// <param name="url">url</param>
        
/// <param name="title">title</param>
        
/// <param name="target">target</param>
        
/// <param name="oldName">超链接原先的值</param>
        
/// <param name="newName">新值</param>
        
/// <returns>操作是否成功</returns>

        public bool update(string url,string title,string target,string oldName,string newName)
        
{
            
try
            
{
                XmlDocument xmlDoc
=new XmlDocument();
                xmlDoc.Load(fileName);
                XmlNode root
=xmlDoc.SelectSingleNode("links");//查找<links>
                XmlNodeList nodeList=root.ChildNodes;//获取bookstore节点的所有子节点
                bool flag=false;
                
foreach(XmlNode xn in nodeList)//遍历所有子节点
                {
                    XmlElement xe
=(XmlElement)xn;//将子节点类型转换为XmlElement类型
                    XmlNodeList nls=xe.ChildNodes;//继续获取xe子节点的所有子节点
                    foreach(XmlNode xn1 in nls)//遍历
                    {
                        XmlElement xe2
=(XmlElement)xn1;//转换类型
                        if(xe2.Name=="name"&&xe2.InnerText==oldName)//如果找到
                        {
                            flag
=true;
                            
break;//找到退出来就可以了
                        }

                    }

                    
if(flag==true)//Format.FormatXml()Format.FormatXml()Format.FormatXml()
                    {
                        nls.Item(
0).InnerText=url;
                        nls.Item(
1).InnerText=title;
                        nls.Item(
2).InnerText=target;
                        nls.Item(
3).InnerText=newName;
                        xmlDoc.Save(fileName);
//保存。
                        info="成功更改友情链接'"+oldName+"'";
                        
break;
                    }

                }

                
if(flag==false)
                
{
                    Add(url,title,target,newName);
                }

                
return true;
            }

            
catch(Exception e)
            
{
                info
=e.Message;
                
return false;
            }

        }


        
/// <summary>
        
/// 更新指定名称的友情链接元素
        
/// </summary>
        
/// <param name="values">元素值集合</param>
        
/// <param name="oldName">超链接名称</param>
        
/// <returns>操作是否成功</returns>

        public bool update(string[] values,string oldName)
        
{
            
try
            
{
                XmlDocument xmlDoc
=new XmlDocument();
                xmlDoc.Load(fileName);
                XmlNodeList nodeList
=xmlDoc.SelectSingleNode("links").ChildNodes;//获取bookstore节点的所有子节点
                bool flag=false;
                
foreach(XmlNode xn in nodeList)//遍历所有子节点
                {
                    XmlElement xe
=(XmlElement)xn;//将子节点类型转换为XmlElement类型
                    if(((XmlElement)(xe.LastChild)).InnerText==oldName)//如果找到   
                    
                        flag
=true;
                        
break;//找到退出来就可以了
                    }

                    
if(flag==true)
                    
{
                        XmlNodeList nls
=xe.ChildNodes;//继续获取xe子节点的所有子节点
                        for(int i=0;i<values.Length;i++)
                        
{
                            nls.Item(i).InnerText
=values[i];
                        }

                        xmlDoc.Save(fileName);
//保存
                        info="成功更改友情链接'"+oldName+"'";
                        
break;
                    }

                }

                
if(flag==false)
                
{
                    Add(values);
                }

                
return true;
            }

            
catch(Exception e)
            
{
                info
=e.Message;
                
return false;
            }

        }


        
/// <summary>
        
/// 从xml文档获取友情链接,返回一个DataSet对象
        
/// </summary>
        
/// <returns>DataSet对象</returns>

        public DataSet GetLinks()
        
{
            
try
            
{
                DataSet ds
=new DataSet("links");
                ds.ReadXml(fileName);
                
return ds;
            }

            
catch(Exception e)
            
{
                info
=e.Message;
                
return null;
            }

        }


        
/// <summary>
        
/// 创建页眉站点导航,一张超链接表格
        
/// </summary>
        
/// <returns>返回超链接表格的html标记代码[string类型]</returns>

        public string SiteNavigation()
        
{
            
try
            
{
                System.Text.StringBuilder sb
=new System.Text.StringBuilder();
                XmlDocument xmlDoc
=new XmlDocument();
                xmlDoc.Load(fileName);
                XmlNodeList nodeList
=xmlDoc.SelectSingleNode("links").ChildNodes;//获取bookstore节点的所有子节点
                int i=0;
                sb.Append(
"<TABLE cellSpacing=1 cellPadding=1 width=100% border=0><TR>");
                
foreach(XmlNode xn in nodeList)//遍历所有子节点
                {
                    i
++;
                    sb.Append(
"<td><a href="+xn.ChildNodes[0].InnerText);
                    sb.Append(
" title="+xn.ChildNodes[1].InnerText);
                    sb.Append(
" target="+xn.ChildNodes[2].InnerText);
                    sb.Append(
" >"+xn.ChildNodes[3].InnerText+"</a></td>");
                    
if(i%columns==0)
                    
{
                        sb.Append(
"</tr><tr>");
                    }

                }

                i
=columns-(i%columns);
                
if(i!=columns)
                
{
                    
for(int j=0;j<i;j++)
                    
{
                        sb.Append(
"<td>&nbsp;&nbsp;&nbsp;</td>");
                    }

                }

                sb.Append(
"</tr></table>");
                
return sb.ToString();
            }

            
catch(Exception e)
            
{
                info
=e.Message;
                
return "";
            }

        }

    }

}