日期:2014-05-18  浏览次数:20463 次

asp.net+XML "修改模块"代码修改
先帖上代码!

C# code

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.Xml;
using System.IO;

public partial class manager_edit_news : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string url = Server.MapPath("../../news.xml");
        string news_id = Request.QueryString["news_id"];
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(url); //加载XML文档
        XmlNode root = xmlDoc.SelectSingleNode("news/content[news_id ='" + news_id + "']");

        if (root != null)
        {
            Title.Text = root.ChildNodes[0].InnerText;
            Date.Text = DateTime.Now.ToString();
            Description.Text = root.ChildNodes[2].InnerText;
            Content.Value = root.ChildNodes[3].InnerText;

        }
    }
    protected void btnSend_Click(object sender, EventArgs e)
    {
        string url = Server.MapPath("../../news.xml");
        string news_id = Request.QueryString["news_id"];
        string title = Request.Form["title"];
        string description = Request.Form["description"];
        string content = Request.Form["Content"];
        //获取行信息的索引字段news_id
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(url); //加载XML文件
        XmlNode root = xmlDoc.SelectSingleNode("news");//查找news节点
        XmlNodeList xnl = root.ChildNodes; //获取news下面所以子节点
        for (int i = 0; i < xnl.Count; i++)
        {
            XmlNodeList xnl11 = xnl.Item(i).SelectNodes("news_id");//查找news_id下面节点集合
            XmlElement xe = (XmlElement)xnl11.Item(0); //news_id节点下面的元素
            if (xe.InnerText == news_id) //获取节点以及所有子集的串联值
            {
                xe.InnerText = news_id;
                xe.ParentNode.ChildNodes.Item(0).InnerText = title;
                xe.ParentNode.ChildNodes.Item(1).InnerText = DateTime.Now.ToString();
                xe.ParentNode.ChildNodes.Item(2).InnerText = description;
                [color=#FF0000]xe.ParentNode.ChildNodes.Item(3).InnerText = content;[/color]
            }
        }
        xmlDoc.Save(url);
        Response.Redirect("manager.aspx");
    }
}



点击修改按钮之后XML文件如下:
XML code

<news originalMenuTitleColor="0xCCCC33" originalMenuDateColor="0x666666" currentMenuTitleColor="0x00CCFF" currentMenuDateColor="0xCCCCCC" arrowOutColor="0x999999" arrowOverColor="0xCCCC33" mainTitleColor="0xCCCC33" menuSpacing="40" textHolderXStart="220" textHolderXFinish="240" mainTextWidth="600" scrollbarX="855">
  <content>
    <title>测试文章</title>
    <date>2008-12-22 12:27:37</date>
    <description>测试文章简介</description>
    <main>&amp;lt;p&amp;gt;111&amp;lt;/p&amp;gt;</main>
    <news_id>1</news_id>
  </content>



我想将修改后的<main>&amp;lt;p&amp;gt;111&amp;lt;/p&amp;gt;</main>节点以
<main><![CDATA[<b>111<b>]]></main>

<![CDATA[]]>这种形式保存在XML节点中,请教高人.CS文件中如何修改?

------解决方案--------------------
XmlWriter writer
writer.WriteCData("");
http://www.cnblogs.com/torome/articles/308824.html
------解决