如何将CDATA添加到xml文件? [英] How do I add CDATA to an xml file?
本文介绍了如何将CDATA添加到xml文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个现有的xml文件,其中包含要显示在网站上的通知。以下是一个摘要:
I have an existing xml file that holds notifications I want to display on my site. A snippet follows:
<contents>
<item>
<![CDATA[
<a style="font-weight: bold;" href="http://engadget.com">Engadget</a>
]]>
</item>
<item>
<![CDATA[
<a style="font-weight: bold;" href="http://cnn.com">CNN</a>
]]>
</item>
</contents>
我正在尝试打开此文档并向其中添加新的项目,但是我可以t:
I'm trying to open this document and add new "items" to it, but I can't:
foreach (string s in notifications)
{
XmlElement newElement = doc.CreateElement("item");
newElement.InnerXml = "<![CDATA[ " + s + " ]]>";
doc.DocumentElement.SelectNodes("/contents")[0].AppendChild(newElement);
}
通知是我用于存储链接的列表。我得到的错误是:
notifications is a List that I'm using to store the links. The error I'm getting is:
']]>'不允许用于字符数据。
由于我的显示方式,通知中必须包含HTML。
The notifications need to contain HTML, because of the way I'm displaying it. Thanks for looking, guys.
推荐答案
尝试使用
newElement.AppendChild(doc.CreateCDataSection(s));
而不是
newElement.InnerXml = "<![CDATA[ " + s + " ]]>";
这篇关于如何将CDATA添加到xml文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文