public class Book { public string title; static void Main() { Book introToVCS = new Book(); introToVCS.title = "Intro to Visual CSharp"; System.Xml.Serialization.XmlSerializer writer = new System.Xml.Serialization.XmlSerializer(introToVCS.GetType()); System.IO.StreamWriter file = new System.IO.StreamWriter("c:\\IntroToVCS.xml"); writer.Serialize(file, introToVCS); file.Close(); } }
下面是MSDN中的代码,你看看 using System; using System.IO; using System.Xml;public class Sample { public static void Main() { XmlDocument doc = new XmlDocument(); doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" + "<title>Pride And Prejudice</title>" + "</book>"); //Create a CData section. XmlCDataSection CData; CData = doc.CreateCDataSection("All Jane Austen novels 25% off starting 3/23!"); //Add the new node to the document. XmlElement root = doc.DocumentElement; root.AppendChild(CData);
Console.WriteLine("Display the modified XML..."); doc.Save(Console.Out); } }
XmlElement body=xd.CreateElement("body");
body.AppendChild(cdata);
xerecord.AppendChild(body);
{
public string title; static void Main()
{
Book introToVCS = new Book();
introToVCS.title = "Intro to Visual CSharp";
System.Xml.Serialization.XmlSerializer writer =
new System.Xml.Serialization.XmlSerializer(introToVCS.GetType());
System.IO.StreamWriter file =
new System.IO.StreamWriter("c:\\IntroToVCS.xml"); writer.Serialize(file, introToVCS);
file.Close();
}
}
<root>
<body>
<![CDATA[asdfq< & html&gt;& ]]>
</body>
</root>
doc.Load("xmlfile");XmlCDataSection cdata=doc.CreateCDataSection(article.Value.ToString());
XmlElement body=doc.CreateElement("body");
body.AppendChild(cdata);
doc.AppendChild(body);doc.Svae("xmlfile");
using System;
using System.IO;
using System.Xml;public class Sample
{
public static void Main()
{
XmlDocument doc = new XmlDocument();
doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" +
"<title>Pride And Prejudice</title>" +
"</book>"); //Create a CData section.
XmlCDataSection CData;
CData = doc.CreateCDataSection("All Jane Austen novels 25% off starting 3/23!"); //Add the new node to the document.
XmlElement root = doc.DocumentElement;
root.AppendChild(CData);
Console.WriteLine("Display the modified XML...");
doc.Save(Console.Out);
}
}
存进去的变成这样了.
可是现在lt;html></html 谢谢在了CDATA里边了,等用XSL转换出来的时候,它就变成了<html></html><> ....
郁闷...大家再帮忙呀...
即可
XmlCDataSection cdata=xd.CreateCDataSection(article.Value.ToString().Trim());
XmlElement body=xd.CreateElement("body");
body.AppendChild(cdata);
xerecord.AppendChild(body);其中
article.Value.ToString().Trim()的内容是<html></html>
当我执行完后,它在XML文件中写入了
<body><![CDATA[<html></html><><><>]]></body>
等我再用XSL显示出来的时候,它就直接把CDATA中的内容显示出来了.没有把&lt;和&gt;转换成 < 和> ...
再帮忙.谢谢.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<head></head>
<body>
<xsl:value-of select="//body" disable-output-escaping="yes"></xsl:value-of>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
------------------
XML:
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="123.xsl"?>
<root>
<body><![CDATA[
<html></html><><><>
]]>
</body>
</root>
<Script Language="C#" Runat="Server">
public void Button1_Click(Object sender,EventArgs e)
{
System.Xml.XmlDocument x = new System.Xml.XmlDocument();
x.LoadXml("<root/>");
Response.Write(Server.HtmlEncode(x.OuterXml));
System.Xml.XmlCDataSection body=x.CreateCDataSection(article.Value);
x.DocumentElement.AppendChild(body);
Response.Write("<hr>");
Response.Write(Server.HtmlEncode(x.OuterXml));
}
</Script>
<html>
<head><title>HtmlButton控件</title></head>
<body>
<form runat="server">
<textarea id=article runat=server><html></html></textarea>
<button id="button1" runat="server" OnServerClick="Button1_Click">提交</button>
</form>
</center>
</body>
</html>
谢谢.
散分.:)