I'm trying to put <!CDATA>
in a specific tag in my XML
file, but the result is <![CDATA[mystring]]>
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() + "]]>";
CDATA sections may be added anywhere character data may occur; they are used to escape blocks of text containing characters which would otherwise be recognized as markup. CDATA sections begin with the string " <! [CDATA[ " and end with the string " ]]> ".
The spec says that the Attribute value must not have an open angle bracket. Open angle brackets and ampersand must be escaped. Therefore you cannot insert a CDATA section.
A CDATA section is used to mark a section of an XML document, so that the XML parser interprets it only as character data, and not as markup. It comes handy when one XML data need to be embedded within another XML document.
Note that CDATA sections should not be used within HTML; they only work in XML.
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..??");
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"));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With