Lucas_Santos
Lucas_Santos

Reputation: 4740

How can I put the <!CDATA> in a XML tag

I'm trying to put <!CDATA> in a specific tag in my XML file, but the result is &lt;![CDATA[mystring]]&gt;

Someone can help me ?

The encoding

XmlProcessingInstruction pi = doc.CreateProcessingInstruction("xml", "version=\"1.0\" encoding=\"utf-8\"");

How I'm doing

texto.InnerText = "<![CDATA[" + elemento.TextoComplementar.ToString() + "]]>";

Upvotes: 5

Views: 10177

Answers (2)

Vishal Suthar
Vishal Suthar

Reputation: 17194

XmlNode xnode = xdoc.SelectSingleNode("entry/entry_status");

XmlCDataSection CData;

InnerText performs whatever escaping is required.

xnode.InnerText = "Hi, How are you..??";

If you want to work with CDATA node then:

CData = doc.CreateCDataSection("Hi, How are you..??");

Upvotes: 5

Andras Zoltan
Andras Zoltan

Reputation: 42363

You haven't explained how you are creating the XML - but it looks like it's via XmlDocument.

You can therefore use CreateCDataSection.

You create the CData node first, supplying the text to go in it, and then add it as a child to an XmlElement.

You should probably consider Linq to XML for working with XML - in my most humble of opinions, it has a much more natural API for creating XML, doing away with the XML DOM model in favour of one which allows you to create whole document trees inline. This, for example, is how you'd create an element with an attribute and a cdata section:

var node = new XElement("root", 
  new XAttribute("attribute", "value"),
  new XCData("5 is indeed > 4 & 3 < 4"));

Upvotes: 4

Related Questions