Reputation:
As we know CDATA cannot be nested, so I like to use the solution provided in Using CDATA inside another CDATA that replace ]]>
with ]]]]><![CDATA[>
.
Therefore
<Root>
<![CDATA[
<AAA>
<![CDATA[
<BBB>hello world</BBB>
]]>
</AAA>
]]>
</Root>
becomes
<Root>
<![CDATA[
<AAA>
<![CDATA[
<BBB>hello world</BBB>
]]]]><![CDATA[>
</AAA>
]]>
</Root>
The XML is the response of my API, which will be used by other programs not under my control.
For .NET, my experiment shows that InnerText
can output text in all CDATA sections.
var Root= doc.SelectNode("/Root");
var cdata = Root.InnerText;
cdata is
<AAA>
<![CDATA[
<BBB>hello world</BBB>
]]>
</AAA>
Does the behavior of .NET comply with any standards? Are there any standards saying how to deal with adjacent CDATA? If my API returns adjacent CDATA, will other programs or programming languages have issue processing it?
Upvotes: 2
Views: 97
Reputation: 9391
This behaviour is absolutely standard compliant and should produce the same result in any XML processor. CDATA sections can be used to escape any character data anywhere (except in another CDATA section) and you can use as many of them as you like, adjacent or not. From the specification:
Definition: CDATA sections may occur anywhere character data may occur; they are used to escape blocks of text containing characters which would otherwise be recognized as markup.
Upvotes: 1