i have an xml like :
<?xml version="1.0" encoding="UTF-8"?> <entry> <entry_id></entry_id> <entry_status></entry_status> </entry>
i am writing data in it like:
XmlNode xnode = xdoc.SelectSingleNode("entry/entry_status"); xnode.InnerText = "<![CDATA[ " + Convert.ToString(sqlReader["story_status"]) + " ]]>" ;
but its change "<" to "<" of CDATA. Please tell me how to fill values in above xml as a CData format.
i know that we can create CDATA like :
XmlNode itemDescription = doc.CreateElement("description"); XmlCDataSection cdata = doc.CreateCDataSection("<P>hello world</P>"); itemDescription.AppendChild(cdata); item.AppendChild(itemDescription);
but my process is to read node of xml and change its value not to append in it. Thanks
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 " ]]> ".
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.
In current Java DOM implementation you can access CDATA simply as text data using e. getTextContent() . See example without type check, cast, e. getData() .
A CDATA section begins with the character sequence <! [CDATA[ and ends with the character sequence ]]>. Between the two character sequences, an XML processor ignores all markup characters such as <, >, and &. The only markup an XML pro-cessor recognizes inside a CDATA section is the closing character sequence ]>.
As described here: msdn
// Create an XmlCDataSection from your document var cdata = xdoc.CreateCDataSection(Convert.ToString(sqlReader["story_status"])); // Append the cdata section to your node xnode.AppendChild(cdata);
Do you really need it to be in CDATA, or do you just want to get the text in there in a way which won't require extra escaping in your code?
InnerText
performs whatever escaping is required, so generally I'd just use
xnode.InnerText = Convert.ToString(sqlReader["story_status"]);
... but if you really want a CDATA node, you can create one yourself as per Nekresh's answer.
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