stovroz
stovroz

Reputation: 7065

Modifying XElement value loses CData type

I've got an XML file with some CData nodes. But when I try and modify them in situ:

var doc = XDocument.Load(filename);
foreach(var el in doc.Descendants("foo"))
{
    el.Value = el.Value.Replace("bar", "baz");
}
doc.Save(filename);

...they all lose their CData types. What's the best way of avoiding that? Thanks!

Upvotes: 0

Views: 1343

Answers (1)

Alexei Levenkov
Alexei Levenkov

Reputation: 100527

CData and plain text nodes are equialent from XML point of view. So the behavior you see is expected - Value never creates CDATA nodes as there is no need for it from XML point of view.

If you must have CData you need to explicitly create one. See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.createcdatasection.aspx for an exaple.

Upvotes: 2

Related Questions