简体   繁体   English

XML CDATA编码

[英]XML CDATA Encoding

I know to use CreateCDataSection to generate a CDATA node. 我知道使用CreateCDataSection来生成CDATA节点。 However, is there any way to insert a CDATA string into my original string? 但是,有什么方法可以将CDATA字符串插入我的原始字符串中? Instead of having the whole node being enclosed by CDATA tag. 而不是整个节点都被CDATA标记包围。 Below is my code. 下面是我的代码。 Please advise, thanks. 请指教,谢谢。

var detail = new StringBuilder();
detail.AppendFormat("<![CDATA[<br />]]>another line: {0}", foo1);
detail.AppendFormat("<![CDATA[<br />]]>another line: {0}", foo2);
detail.AppendFormat("<![CDATA[<br />]]>another line: {0}", foo3);

var xmlOutput = new XElement("Detail",detail);

Instead of CDATA, use &lt; 代替CDATA,使用&lt; for the '<'. 为“ <”。

detail.AppendFormat("&lt;br />another line: {0}", foo1);
detail.AppendFormat("&lt;br />another line: {0}", foo2);
detail.AppendFormat("&lt;br />another line: {0}", foo3);

var xmlOutput = new XElement("Detail", detail);

are you looking for this. 你在找这个吗

        var br = new XCData("<br />");
        detail.AppendFormat("{0}another line: {1}", br, foo1);
        detail.AppendFormat("{0}another line: {1}", br, foo2);
        detail.AppendFormat("{0}another line: {1}", br, foo3);            
        var xmlOutput = new XElement("Detail", detail);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM