asp.net XML远程编辑演示

原创 2012年03月30日 10:49:13

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

远程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;
    }
}



asp.net 如何获取远程的xml数据

步骤一:创建RSS通用类C#代码using System; using System.IO; using System.Data; using System.Xml; using System.Col...

基于asp.net + easyui框架,一步步学习easyui-datagrid——实现添加、编辑、删除(三)

上周就想更新这篇博客了,但是在做这个界面的过程中,遇到了很多的问题,一时没有解决方案,现在想到了一个,虽然还有点不尽完美,但是能实现项目的基本功能。在下面我会给大家说一下我遇到的困难,并提供实现的代码...

Asp.net 使用weboffice实现Word在线编辑

项目中客户需要在页面中实现Word的在线编辑,Word写保护等功能。之前写了篇文章,用的是dsoframer.ocx,参考的都是网络上的资源。用的时候页面卡, 而且word2003和word20...

Asp.net的GridView控件实现单元格可编辑

最近做一个功能,考虑到用户使用方便,减少弹出页面,采用点“编辑”按钮无需弹出页面直接当前行的单元格内容就能编辑。进入页面显示如下图: 点“编辑”按钮后显示如下图: 编号为1的“星期”和“是...

检查编辑的方法和视图(ASP.NET MVC3系列文章六)

原址:http://www.asp.net/mvc/tutorials/getting-started-with-aspnet-mvc3/getting-started-with-mvc3-part6...

分享[ASP.NET] SQL语句统计每天、每月、每年的数据 编辑:吴立星

SQL语句统计每天、每月、每年的数据 1、每年 select year(ordertime) 年, sum(Total) 销售合计 from 订单表 group by year(orde...

asp.net JQuery Ajax WebService Gridview 任意 双击 某一列 编辑修改

写一个WebService方法GetAuto,xiang

ASP.NET Excel导入SQLserver 数据库中 编辑:吴立星

上次修改时间:2009年8月2日  本次修改时间: 2009年9月7日  修改人: 吴立星  一 .aspx 页          Excel导入SQL数据库         ...

ASP.NET2.0下含有DropDownList的GridView编辑、删除的完整例子

在MVC中的功能化编辑(ASP.NET)

源地址: http://forums.asp.net/t/1933437.aspx?edit+functionality+in+MVC 下面这段代码能被很好的执行(多个图片),但是当我编辑没有上传任...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:asp.net XML远程编辑演示
举报原因:
原因补充:

(最多只允许输入30个字)