关闭

asp.net XML远程编辑演示

标签: xmlasp.netsessiontextboxserverasp
1847人阅读 评论(0) 收藏 举报
分类:

本示例已上传,下载地址:

远程XML文件编辑示例


XML文档格式:

<?xml version="1.0" encoding="utf-8" ?>
<CONTENT_ROOT>
  <ItemRecordsList type="1" description="首页">
    <ItemRecords TITLE="南百美美购实习生招聘启事"
 HREF="asp/v.do?id=13861&cid=61"/>
    <ItemRecords TITLE="别堆积 三洋洗衣机震撼价仅售1599"
 HREF="/BaseNews/ShowDM.aspx?id=102"/>
    <ItemRecords TITLE="农行网银支付专享通道 全场3折起"
 HREF="ZT_NongHangCX.aspx"/>
    <ItemRecords TITLE="马来西亚王室专用 芭菲洗衣液进驻"
 HREF="BaseNews/ShowDM.aspx?id=85"/>
    <ItemRecords TITLE="汇美舍开春特惠 满100减20"
 HREF="BaseNews/ShowDM.aspx?id=91"/>
    <ItemRecords TITLE="超市特价专场 低至4折"
 HREF="BaseNews/ShowDM.aspx?id=93"/>
    <ItemRecords TITLE="泊美新品 植物奢白乳液仅售145元"
 HREF="products.aspx?spid=10629"/>
    <ItemRecords TITLE="欧珀莱时光锁柔润水 满减仅190元"
  HREF="products.aspx?spid=9437"/>
  </ItemRecordsList>

  <ItemRecordsList type="2" description="日用品">
    <ItemRecords TITLE="组合套装,打包购买更划算"
 HREF="SearchPagePdList.aspx?spflcode=090509"/>
    <ItemRecords TITLE="不用注册 电话也能下单了"
 HREF="asp/v.do?id=12250&cid=67"/>
    <ItemRecords TITLE="广西特产入驻 4元起低价选购"
 HREF="SearchPagePdList.aspx?spflcode=090138"/>
    <ItemRecords TITLE=" 品红酒 王朝长城张裕随你挑"
 HREF="SearchPagePdList.aspx?spflcode=090135"/>
    <ItemRecords TITLE=" 沙宣护发造型23元起"
 HREF="SearchPagePdList.aspx?searchKey=%u6C99%u5BA3"/>
    <ItemRecords TITLE=" 安儿乐 给宝宝最好的纸尿裤"
 HREF="SearchPagePdList.aspx?searchKey=%u5B89%u513F%u4E50"/>
    <ItemRecords TITLE=" 奥妙高浓度洗衣液 3包仅售17.8"
 HREF="products.aspx?spid=8416"/>
    <ItemRecords TITLE="做魅力“膜”女 面膜3.3元起 "
 HREF="SearchPagePdList.aspx?searchKey=%u9762%u819C"/>
  </ItemRecordsList>


  <ItemRecordsList type="3" description="家电">
    <ItemRecords TITLE="家电下乡 康佳电视42寸仅售2999"
 HREF="products.aspx?spid=8267"/>
    <ItemRecords TITLE="创维3D电视47寸仅售5999"
 HREF="products.aspx?spid=8176"/>
    <ItemRecords TITLE=" 海尔双门冰箱186KB仅1399"
 HREF="products.aspx?spid=7721"/>
    <ItemRecords TITLE=" 容声对开门冰箱仅售3999"
 HREF="products.aspx?spid=7809"/>
    <ItemRecords TITLE="万和天然气热水器仅售1698"
 HREF="products.aspx?spid=8291"/>
    <ItemRecords TITLE="西门子滚筒洗衣机 仅售4599"
 HREF="products.aspx?spid=7909"/>
    <ItemRecords TITLE="美的波轮洗衣机仅1498"
 HREF="products.aspx?spid=7874"/>
    <ItemRecords TITLE="飞利浦三合一搅拌机仅售289"
 HREF="products.aspx?spid=9626"/>
  </ItemRecordsList>
</CONTENT_ROOT>

 
页面文件:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>XML编辑器</title>
</head>
<body>
    <form id="form1" runat="server">
    <div style="width: 100%;">
        <div style="width: 300px; float: left;">
            <asp:TreeView ID="TreeView1" runat="server" DataSourceID="XmlDataSource1" BorderStyle="Solid"
                Width="300px" OnSelectedNodeChanged="TreeView1_SelectedNodeChanged">
                <SelectedNodeStyle BackColor="#FFCCFF" />
                <DataBindings>
                    <asp:TreeNodeBinding DataMember="ItemRecordsList" TextField="description" />
                    <asp:TreeNodeBinding DataMember="ItemRecords" TextField="TITLE" ValueField="HREF" />
                </DataBindings>
                <NodeStyle ForeColor="Black" />
            </asp:TreeView>
            <asp:XmlDataSource ID="XmlDataSource1" runat="server" 
                XPath="/CONTENT_ROOT/ItemRecordsList" EnableCaching="False" >
            </asp:XmlDataSource>
        </div>
        <div style="width: 400px; float: left; margin-left: 10px; border-style: solid; border-width: 1px;">
            <table style="margin: 0 auto; width: 80%; margin-top: 50px; margin-bottom: 50px;">
                <tr>
                    <td width="60" align="right">
                        标题:
                    </td>
                    <td>
                        <asp:TextBox ID="txtTitle" runat="server" Width="250px"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td align="right">
                        链接:
                    </td>
                    <td>
                        <asp:TextBox ID="txtHREF" runat="server" Width="250px"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td colspan="2" align="center">
                        <asp:Button ID="btnEdit" runat="server" Text="修改"
                            onclick="btnEdit_Click" />
                        <asp:Button ID="btnUP" runat="server" Text="上移" Style="margin-left: 10px;" 
                            onclick="btnUP_Click" />
                        <asp:Button ID="btnDown" runat="server" Text="下移" Style="margin-left: 10px;" 
                            onclick="btnDown_Click" />
                    </td>
                </tr>
                <tr>
                    <td colspan="2" align="center">
                        <asp:Button ID="btnSave" runat="server" Text="保存" OnClick="btnSave_Click" 
                            Enabled="False" />
                        <asp:Button ID="btnCancel" runat="server" Text="取消" Style="margin-left: 10px;" OnClick="btnCancel_Click" />                        
                    </td>
                </tr>
            </table>
        </div>
    </div>
    </form>
</body>
</html>
 
页面代码:
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Text;
using System.Web.Services.Description;

public partial class _Default : Page
{
    private string path = string.Empty;
    private XmlDocument xmldoc = null;  //由于 XmlDataSource.Save() 方法不支持远程保存,将远程XML文档存入Session便于保存
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindXML();
        }
    }

    protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
    {
        if (TreeView1.SelectedNode.Depth == 1)
        {
            txtTitle.Text = TreeView1.SelectedNode.Text;
            txtHREF.Text = TreeView1.SelectedNode.Value;
            btnUP.Enabled = true;
            btnDown.Enabled = true;
            btnEdit.Enabled = true;
        }
        else
        {
            txtTitle.Text = "";
            txtHREF.Text = "";
            btnUP.Enabled = false;
            btnDown.Enabled = false;
            btnEdit.Enabled = false;
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        xmldoc = Session["news_xml"] as XmlDocument;
        byte[] bytes = Encoding.UTF8.GetBytes(xmldoc.OuterXml);
        //保存文件
        if (SaveFile(bytes))
        {
            this.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('保存成功!');", true);
            btnSave.Enabled = false;
        }
    }
    /// <summary>
    /// 保存文件
    /// </summary>
    /// <param name="bytes"></param>
    /// <returns></returns>
    private bool SaveFile(byte[] bytes)
    {
        //throw new NotImplementedException();
        //这里调用远程主机的 webservice 方法上传文件
        return true;
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        txtTitle.Text = "";
        txtHREF.Text = "";
        BindXML();
        btnSave.Enabled = false;
    }

    private void BindXML()
    {
        //远程路径 带参数避免文件被缓存
        path = "http://192.168.1.100/Data/Data.xml?d=" + DateTime.Now.Ticks;
        //为便于演示,用本地路径
        path = "~/App_Data/Data.xml";
        XmlDataSource1.EnableCaching = false; //数据源不缓存文件
        XmlDataSource1.DataFile = path;
        XmlDataSource1.DataBind();
        xmldoc = XmlDataSource1.GetXmlDocument();
        Session["news_xml"] = xmldoc;
    }

    protected void btnUP_Click(object sender, EventArgs e)
    {
        if (TreeView1.SelectedNode != null && TreeView1.SelectedNode.Depth == 1)
        {
            TreeNode parent = TreeView1.SelectedNode.Parent;
            TreeNode move = TreeView1.SelectedNode;
            int index = parent.ChildNodes.IndexOf(TreeView1.SelectedNode);
            if (index > 0)
            {
                index--;
                xmldoc = Session["news_xml"] as XmlDocument;
                XmlNode node = GetXmlNode(xmldoc);
                parent.ChildNodes.AddAt(index, move);
                move.Selected = true;
                XmlNode parentNode = node.ParentNode;
                parentNode.InsertBefore(node, parentNode.ChildNodes[index]);
                Session["news_xml"] = xmldoc;
                btnSave.Enabled = true;
            }
        }
    }

    protected void btnDown_Click(object sender, EventArgs e)
    {
        if (TreeView1.SelectedNode != null && TreeView1.SelectedNode.Depth == 1)
        {
            TreeNode parent = TreeView1.SelectedNode.Parent;
            TreeNode move = TreeView1.SelectedNode;
            int index = parent.ChildNodes.IndexOf(TreeView1.SelectedNode);
            if (index < parent.ChildNodes.Count - 1)
            {
                index++;
                xmldoc = Session["news_xml"] as XmlDocument;
                XmlNode node = GetXmlNode(xmldoc);
                parent.ChildNodes.AddAt(index, move);
                move.Selected = true;
                XmlNode parentNode = node.ParentNode;
                parentNode.InsertAfter(node, parentNode.ChildNodes[index]);
                Session["news_xml"] = xmldoc;
                btnSave.Enabled = true;
            }
        }
    }

    protected void btnEdit_Click(object sender, EventArgs e)
    {
        if (TreeView1.SelectedNode != null)
        {
            TreeView1.SelectedNode.Text = txtTitle.Text.Trim();
            TreeView1.SelectedNode.Value = txtHREF.Text.Trim();
            xmldoc = Session["news_xml"] as XmlDocument;
            XmlNode node = GetXmlNode(xmldoc);
            node.Attributes["TITLE"].Value = txtTitle.Text.Trim();
            node.Attributes["HREF"].Value = txtHREF.Text.Trim();
            Session["news_xml"] = xmldoc;
            btnSave.Enabled = true;
        }
    }
    /// <summary>
    /// 取得树节点对应的XML结点
    /// </summary>
    /// <param name="xml"></param>
    /// <returns></returns>
    private XmlNode GetXmlNode(XmlDocument xml)
    {
        TreeNode select = TreeView1.SelectedNode;
        TreeNode parent = select.Parent;
        XmlNodeList list = xml.DocumentElement.ChildNodes[TreeView1.Nodes.IndexOf(parent)].ChildNodes;
        XmlNode node = list[parent.ChildNodes.IndexOf(select)];
        return node;
    }
}



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:602865次
    • 积分:6677
    • 等级:
    • 排名:第3504名
    • 原创:103篇
    • 转载:123篇
    • 译文:0篇
    • 评论:49条
    最新评论