LINQ to XML:如何替换XCData的内容

using System;
using System.Linq;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //写入CDATA元素块
            var doc =
                new XElement("Test",
                    new XElement("User",
                        new XAttribute("name", "chenxizhang"),
                        new XCData("这是XCData的内容,可以包含特殊字符,例如< 或者 >")));
            Console.WriteLine("写入CData元素");
            Console.WriteLine(doc.ToString());


            

doc.Element("User").FirstNode.ReplaceWith(new XCData("新的文本内容"));

            Console.Write(doc.Element("User").Value);

        }
    }
}
posted @ 2010-01-16 22:58  陈希章  阅读(602)  评论(0编辑  收藏  举报