asp.net 操作 xml格式网站地图 添加删除修改!(简单版)

xml 格式

<?xml version="1.0" encoding="utf-8"?>
<SiteMap>
  
<node Value="32" Text="revengeboy" NavigateUrl="322">
    
<node Value="1234" Text="1234" NavigateUrl="123">
    
</node>
    
<node Value="20070702023420" Text="123" NavigateUrl="123">
    
</node>
  
</node>
  
<node Value="14" Text="123131ooi" NavigateUrl="3123">
    
<node Value="12322" Text="tt00" NavigateUrl="tt">
      
<node Value="123" Text="tt8890===" NavigateUrl="ttii">
        
<node Value="122" Text="5555" NavigateUrl="555">
          
<node Value="1222" Text="5566" NavigateUrl="55">
          
</node>
        
</node>
      
</node>
    
</node>
  
</node>
  
<node Value="20070702024023" Text="73133" NavigateUrl="73133">
  
</node>
</SiteMap>


前台页面

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ajaxTabs.ascx.cs" Inherits="Tabs_ajaxTabs" %>

      
<style type="text/css">
body 
{
     scrollbar
-face-color: #EDEDF3;
     scrollbar
-highlight-color: #ffffff;
     scrollbar
-shadow-color: #93949F;
     scrollbar
-3dlight-color: #EDEDF3;
     scrollbar
-arrow-color: #082468;
     scrollbar
-track-color: #F7F7F9;
     scrollbar
-darkshadow-color: #EDEDF3;
     
     font
-size: 9pt;
     color: #
003366;
     overflow:auto;
 }


/**//**//**//*绿色按钮样式*/
.greenButtonCss 
{
    font
-family: "Tahoma""宋体";
    font
-size: 9pt; color: #0066cc;
    border: 1px #93bee2 solid;
    BORDER
-BOTTOM: #93bee2 1px solid; 
    BORDER
-LEFT: #93bee2 1px solid; 
    BORDER
-RIGHT: #93bee2 1px solid; 
    BORDER
-TOP: #93bee2 1px solid;
    background
-image:url(../Images/greenbuttonbg.gif);
    background
-color: #ffffff;
    CURSOR: hand;
    font
-style: normal ;
}


 
/**//**//**//*蓝色按钮样式*/
 .blueButtonCss 
{
     font
-family: "Tahoma""宋体";
     font
-size: 9pt; color: #003366;
     border: 0px #93bee2 solid;

     background
-image:url(../Images/blue_button_bg.gif);
     background
-color: #ffffff;
     CURSOR: hand;
     font
-style: normal ;
 }

 
/*下拉选择框显示风格
************************************
*/

SELECT
{
    BORDER
-RIGHT: #000000 1px solid; 
    BORDER
-TOP: #FFFFFF 1px solid; 
    FONT
-SIZE: 12px; 
    BORDER
-LEFT: #FFFFFF 1px solid;
    COLOR:#
003366
    BORDER
-BOTTOM: #000000 1px solid; 
    BACKGROUND
-COLOR: #f4f4f4;
}
 

/*多行文本框样式
****************************************************
*/

.MultiEditBox
{
    background: #f8f8f8;
    border
-bottom: #B7B7B7 1px solid;
    border
-left: #B7B7B7 1px solid;
    border
-right: #B7B7B7 1px solid;
    border
-top: #B7B7B7 1px solid;
    color: #
000000;
    cursor: text;
    font
-family: "Arial";
    font
-size: 9pt;
    padding: 1px;
}

/*无框 文本框显示样式**/
.clarity
{
    BORDER
-RIGHT: #ffffff 0px solid; 
    BORDER
-TOP: #ffffff 0px solid;
    FONT
-SIZE: 9pt; 
    BORDER
-LEFT: #ffffff 0px solid; 
    BORDER
-BOTTOM: #ffffff 0px solid; 
}


</style> 
   
<asp:XmlDataSource ID="XmlDataSource1" runat="server"></asp:XmlDataSource>
&nbsp; 
<table>
    
<tr>
        
<td style="width: 100px" valign="top">
            
<asp:TreeView ID="TreeView1" runat="server" ImageSet="Arrows" OnSelectedNodeChanged="TreeView1_SelectedNodeChanged1" ShowLines="True">
                
<ParentNodeStyle Font-Bold="False" />
                
<HoverNodeStyle Font-Underline="True" ForeColor="#5555DD" />
                
<SelectedNodeStyle Font-Underline="True" HorizontalPadding="0px"
                    VerticalPadding
="0px" ForeColor="#5555DD" />
                
<NodeStyle Font-Names="Verdana" Font-Size="8pt" ForeColor="Black" HorizontalPadding="5px"
                    NodeSpacing
="0px" VerticalPadding="0px" />
                
<DataBindings>
                    
<asp:TreeNodeBinding DataMember="node" TargetField="NavigateUrl" TextField="Text" ToolTipField="Value" />
                    
<asp:TreeNodeBinding DataMember="node" TargetField="NavigateUrl" TextField="Text" ToolTipField="Value" />
                
</DataBindings>
            
</asp:TreeView>
        
</td>
        
<td style="width: 100px" valign="top">
            
<asp:Panel ID="Panel1" runat="server" Height="20px" Visible="false" width="243">
                
<table width = 100% bgcolor="whitesmoke">
                    
<tr>
                        
<td width = 70%>
                
<asp:Button ID="BtnTestAdd" runat="server" CssClass="blueButtonCss" OnClick="BtnTestAdd_Click"
                    Text
="添加" />
                
<asp:Button ID="BtnTestUpdate" runat="server" CssClass="blueButtonCss" OnClick="BtnTestUpdate_Click"
                    Text
="修改" />
                
<asp:Button ID="BtnDeleteOne" runat="server" CssClass="blueButtonCss" OnClick="BtnDeleteOne_Click"
                    Text
="删除" /></td>
                        
<td width = 30% align="right" valign="top">
                
<asp:ImageButton ID="ImageButton1" runat="server" OnClick="ImageButton1_Click" ImageUrl="~/Image/close.GIF" /></td>
                    
</tr>
                
</table>
            
</asp:Panel>
        
<asp:Panel ID="PanAdd" runat="server" Height="50px" Visible="false" Width="125px">
                
&nbsp;<table style="width: 243px">
                    
<tr>
                        
<td align="center" colspan="2" style="height: 21px">
                            您是选择在
"<asp:Label ID="LabPrentText" runat="server" Text="Label"></asp:Label>"节点下添加</td>
                    
</tr>
                    
<tr>
                        
<td style="width: 66px">
                            节点名:
</td>
                        
<td style="width: 100px">
                            
<asp:TextBox ID="TxtAddText" runat="server"></asp:TextBox></td>
                    
</tr>
                    
<tr>
                        
<td style="width: 66px; height: 26px">
                            Url:
                        
</td>
                        
<td style="width: 100px; height: 26px">
                            
<asp:TextBox ID="TxtAddNavigateUrl" runat="server"></asp:TextBox></td>
                    
</tr>
                    
<tr>
                        
<td align="center" colspan="2" style="height: 22px">
                            
<asp:Button ID="BtnAdd" runat="server" CssClass="greenButtonCss" OnClick="BtnAdd_Click"
                                Text
="添加" /></td>
                    
</tr>
                
</table>
            
</asp:Panel>
            
<asp:Panel ID="PanUpdate" runat="server" Height="50px" Visible="false" Width="125px">
                
<table style="width: 246px">
                    
<tr>
                        
<td style="width: 142px; height: 25px">
                            标题:
</td>
                        
<td style="width: 100px; height: 25px">
                            
<asp:TextBox ID="TxtText" runat="server"></asp:TextBox></td>
                    
</tr>
                    
<tr>
                        
<td style="width: 142px; height: 26px">
                            节点名:
</td>
                        
<td style="width: 100px; height: 26px">
                            
<asp:TextBox ID="TxtPath" runat="server"></asp:TextBox></td>
                    
</tr>
                    
<tr>
                        
<td style="width: 142px; height: 26px">
                            Url:
</td>
                        
<td style="width: 100px; height: 26px">
                            
<asp:TextBox ID="TxtNavigateUrl" runat="server"></asp:TextBox>
                        
</td>
                    
</tr>
                    
<tr>
                        
<td align="center" colspan="2">
                            
&nbsp; &nbsp;&nbsp;
                            
<asp:Button ID="BtnUpdate" runat="server" CssClass="greenButtonCss" OnClick="BtnUpdate_Click"
                                Text
="修改" />
                        
</td>
                    
</tr>
                
</table>
            
</asp:Panel>
        
</td>
    
</tr>
</table>
&nbsp;&nbsp;







后台代码
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Data.SqlClient;
using System.Xml;
using System.Xml.XPath;
using System.Runtime.InteropServices;

public partial class Tabs_ajaxTabs : System.Web.UI.UserControl
{
    
string strReadPath = "C:\\SiteMap\\siteMap.xml";
    
string strSavePath = "C:\\SiteMap\\siteMap.xml";
 
    [DllImport(
"kernel32.dll")]
    
static extern bool SetFileAttributes(string lpFileName, uint dwFileAttributes);


    
protected void Page_Load(object sender, EventArgs e)
    
{
        
if (!Page.IsPostBack)
        
{
            TreeviewBind(XmlDataSource1, TreeView1, strReadPath);
        }

    }

  
    
void TreeviewBind(XmlDataSource xds,TreeView tv,string path)
    
{
        xds.DataFile 
= path;
        tv.DataSource 
= xds;
        tv.DataBind();
    }

    
protected void BtnAdd_Click(object sender, EventArgs e)//单个按扭 控制多级添加
    {
        
try
        
{           
            XmlDocument doc 
= new XmlDocument();
            doc.Load(strReadPath);
            XPathNavigator navigator 
= doc.CreateNavigator();
            
foreach (XPathNavigator nav in navigator.Select((string)ViewState["NodePath"]))
            
{
                
if (nav.HasAttributes == true)
                
{
                    nav.MoveToFirstAttribute();
                    
if (nav.Name == "Value")
                    
{
                        
if (nav.Value == (string)ViewState["GetValue"])
                        
{
                            nav.MoveToParent();
//重新回到结点                      

                            nav.AppendChild(
"<node Value=\"" + DateTime.Now.ToString("yyyyMMddhhmmss") + "\" Text=\"" + TxtAddText.Text + "\" NavigateUrl=\"" + TxtAddNavigateUrl.Text + "\"></node>");//添加到<SiteMap>节点中

                            SetFileAttributes(strSavePath, (
uint)0);//修改文件属性,去掉所有属性!
                            SetFileAttributes(strSavePath, (uint)128); // FILE_ATTRIBUTE_NORMAL
                            doc.Save(strSavePath);
                            Response.Redirect(Request.RawUrl);
                        }

                    }

                }

                
else
                
{
                    nav.AppendChild(
"<node Value=\"" + DateTime.Now.ToString("yyyyMMddhhmmss") + "\" Text=\"" + TxtAddText.Text + "\" NavigateUrl=\"" + TxtAddNavigateUrl.Text + "\"></node>");//添加到<SiteMap>节点中
                    SetFileAttributes(strSavePath, (uint)0);//修改文件属性,去掉所有属性!
                    SetFileAttributes(strSavePath, (uint)128); // FILE_ATTRIBUTE_NORMAL
                    doc.Save(strSavePath);
                    Response.Redirect(Request.RawUrl);
                }

                
while (nav.MoveToNext()) ;
            }


        }

        
catch (Exception)
        
{

            
throw;
        }

    }

    
protected void BtnUpdate_Click(object sender, EventArgs e)
    
{
        
try
        
{
            XmlDocument xmldoc 
= new XmlDocument();
            xmldoc.Load(strReadPath);
            XPathNavigator navigator 
= xmldoc.CreateNavigator();
            
foreach (XPathNavigator nav in navigator.Select((string)ViewState["NodePath"]))//通过导航 找到根节点,并读出其子节点列表 
            {
                
if (nav.HasAttributes == true)
                
{
                    nav.MoveToFirstAttribute();
                    
if (nav.Name == "Value")
                    
{
                        
if (nav.Value == (string)ViewState["GetValue"])
                        
{
                            
//nav.SetValue(this.TxtText.Text);//将TxtText里的内容保存到属性中
                            nav.MoveToNextAttribute();//移到下一个属性
                            nav.SetValue(this.TxtText.Text);//将TxtNavigateUrl里的内容保存到属性中
                            nav.MoveToNextAttribute();//移到下一个属性
                            nav.SetValue(this.TxtNavigateUrl.Text);//将TxtNavigateUrl里的内容保存到属性中
                        }

                    }

                }
 while (nav.MoveToNext()) ;
            }

            SetFileAttributes(strSavePath, (
uint)0);//修改文件属性,去掉所有属性!
            SetFileAttributes(strSavePath, (uint)128); // FILE_ATTRIBUTE_NORMAL
            xmldoc.Save(strSavePath);
            TreeviewBind(XmlDataSource1, TreeView1, strReadPath);
            Response.Redirect(Request.RawUrl);   
        }

        
catch (Exception)
        
{            
            
throw;
        }


    }

    
protected void BtnDeleteOne_Click(object sender, EventArgs e)
    
{
        
try
        
{
            XmlDocument xmldoc 
= new XmlDocument();
            xmldoc.Load(strReadPath);

    # region 
//不完善代码
            
//XmlNodeList xnl = xmldoc.SelectSingleNode("SiteMap").ChildNodes;//找到根节点,并读出其子节点列表 
            
//foreach (XmlNode xn in xnl)
            
//{

            
//    XmlElement xe = (XmlElement)xn;
            
//    if (xe.GetAttribute("Text") == "123")
            
//    {
            
//        xe.ParentNode.RemoveChild(xe);//删除该节点的全部内容   
            
//        //xmldoc.DocumentElement.RemoveChild.RemoveChild(xe);
            
//    }
            
//


            
//XmlNode xn = xmldoc.SelectSingleNode((string)ViewState["NodePath"]);//找到根节点,并读出其子节点列表 
            
//XmlElement xe = (XmlElement)xn;
            
//    if (xe.GetAttribute("Text") == "123")
            
//    {
            
//        xe.ParentNode.RemoveChild(xe);//删除该节点的全部内容   
            
//    }

    # endregion  

            XPathNavigator navigator 
= xmldoc.CreateNavigator();
            
foreach (XPathNavigator nav in navigator.Select((string)ViewState["NodePath"]))//通过导航 找到根节点,并读出其子节点列表 
            {
                
if (nav.HasAttributes == true)
                
{
                    nav.MoveToFirstAttribute();
                    
if (nav.Name == "Value")
                    
{
                        
if (nav.Value == (string)ViewState["GetValue"])
                        
{
                            nav.MoveToParent();
//重新回到结点  
                            nav.DeleteSelf();//删除自己                        
                        }

                    }

                }
 while (nav.MoveToNext()) ;
            }

            SetFileAttributes(strSavePath, (
uint)0);//修改文件属性,去掉所有属性!
            SetFileAttributes(strSavePath, (uint)128); // FILE_ATTRIBUTE_NORMAL
            xmldoc.Save(strSavePath);
            Response.Redirect(Request.RawUrl);
        }

        
catch (Exception)
        
{
            
            
throw;
        }


    }

   
 TreeNode tn 
= new TreeNode();
    
protected void TreeView1_SelectedNodeChanged1(object sender, EventArgs e)
    
{
        
        
this.ImageButton1.Visible = true;
        
this.Panel1.Visible = true;
        
this.PanAdd.Visible = false;
        
this.PanUpdate.Visible = false;
        tn 
= TreeView1.SelectedNode;
        ViewState[
"GetDelText"= tn.Text;
        ViewState[
"GetNavigateUrl"= tn.Target.ToString();
        ViewState[
"NodePath"= tn.ValuePath;
        ViewState[
"GetValue"= tn.ToolTip;

        
string path = "/node";
        
string rootpath = "SiteMap";
        
int i = tn.ValuePath.Split('/').Length - 1;
        
for (int j = 0; j < i; j++)
        
{
            rootpath 
= rootpath + path;
        }

        ViewState[
"NodePath"= rootpath;
        
this.TxtText.Text = (string)ViewState["GetDelText"];
        
this.TxtPath.Text = (string)ViewState["NodePath"];
        
this.TxtNavigateUrl.Text = (string)ViewState["GetNavigateUrl"];
        
//this.TxtValue.Text = (string)ViewState["GetValue"];

    }


    
protected void BtnTestAdd_Click(object sender, EventArgs e)
    
{
        
this.LabPrentText.Text = (string)ViewState["GetDelText"];
        
this.PanAdd.Visible = true;
        
this.PanUpdate.Visible = false;
    }

    
protected void BtnTestUpdate_Click(object sender, EventArgs e)
    
{
        
this.PanAdd.Visible = false;
        
this.PanUpdate.Visible = true;
    }

    
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    
{
        
this.PanAdd.Visible = false;
        
this.PanUpdate.Visible = false;
        
this.Panel1.Visible = false;
        
this.ImageButton1.Visible = false;
    }



}

posted on 2007-07-03 09:45  RevengeBoy  阅读(1036)  评论(2编辑  收藏  举报

导航