How to serialize a string as CDATA in .NET
If you want to serialize an object that contains HTML data in one of its properties, you might want to make sure the HTML ends up in a CDATA section to prevent parsing errors.
Unfortunately, you can’t just put <![CDATA[ and ]]> around your HTML string. Also, the .NET framework does not provide an attribute that tells the serializer to make a string into a CDATA section.
However, you can declare a property of the type XMLCDataSection to get the desired result.
Here’s an example:
using System.Xml; using System.Xml.Serialization; public MyTestClass { MyTestClass() { } //Private variable that will contain the HTML: private string html; //This is the property that will be serialized as a CDATA section: public XmlCDataSection MyHtml { get { XmlDocument doc = new XmlDocument(); return doc.CreateCDataSection(html); } set { html = value.ToString(); } } }
This class contains a private variable html that contains the original HTML. A public property MyHtml that turns the contents of this variable into a CDATA section and vice versa. Now, you can make an instance of this class and serialize it to XML:
MyTestClass test = new MyTestClass(); test.MyHtmlText = "<b>Hello, <i>world</i>!</b>"; XmlTextWriter xmlwriter = new XmlTextWriter("output.xml", Encoding.UTF8); XmlSerializer serializer = new XmlSerializer(typeof(MyTestClass)); serializer.Serialize(xmlwriter, test);
Now you should have a file output.xml containing your CDATA section:
<MyHtml>
<![CDATA[
<b>Hello, <i>world</i>!</b>
]]>
</MyHtml>
Posted: March 30th, 2008 under .NET.
Comments: 11
Comments
Pingback from Websites tagged “cdata” on Postsaver
Time: September 21, 2008, 9:02
[…] - How to serialize a string as CDATA in .NET saved by squaros2008-09-10 - الناف بار الإضافي على كيفك انت saved by […]
Comment from John Fowler
Time: October 2, 2008, 17:10
Thanks for the help on this. BTW: I don’t think the value.ToString() is correct. I think the setter should be:
set
{
html = value.Value;
}
Thanks again!
john…
Comment from Dan
Time: April 30, 2012, 0:53
I think this won’t work because of implicit conversion of assigning a string to XMLCDataSection data type. I tried it, it doesn’t compile.
test.MyHtmlText = “<b>Hello, <i>world</i…”
Thanks for the effort sharing though.
Comment from arjendk
Time: May 1, 2012, 13:47
Thanks for your comment Dan! Do note that this example is from a few years back and was made for .NET 2.0, so it might work differently on newer versions. Also note that you might have to HTML encode your HTML text as in the example.
Comment from garlic benefits
Time: April 24, 2013, 10:33
Alpha-lipoic acid in particular is a powerful antioxidant
- this is its main reason for inclusion within the stack - and to preserve
its potency, it needs to protected within
an opaque capsule that admits neither light nor liquid, and that doesn’t require the addition of binders or additives. When purchasing raw garlic, choose plump, firm, white bulbs with the papery skin still intact on the bulb. Whole cooked garlic cloves are quite mild, with a nutty flavor.
Comment from ginger
Time: April 27, 2013, 11:41
Ginger is known to inhibit the production of cytokines, which are chemicals that modulate the immune response and promote inflammation.
Ginger is equipped with the capability of preventing prostaglandins, which
helps in the reduction of pain and swelling in blood vessels, caused due to migraine.
Its effectiveness against headaches has been documented.
Comment from ブランド ヴィトン
Time: May 3, 2013, 5:42
Make generally emcees work considering that quick as available.
Bearing that in mind Louis vuitton isn’t cheap, buying online the real deal bags can gain a bundle. A good replica will checking out a little expensive than usual. Consequently, on ones succeeding Lv back pack find, use one particular internet in addition to prevent. %url%
Comment from Colon Cleanse Recipe
Time: May 15, 2013, 19:20
Hello, i feel that i saw you visited my web site thus i came to return the choose?
.I am trying to in finding issues to improve my website!
I assume its ok to make use of a few of your concepts!!
Comment from Lance
Time: May 18, 2013, 0:09
I’m not sure exactly why but this blog is loading extremely slow for me. Is anyone else having this problem or is it a problem on my end? I’ll check back
later and see if the problem still exists.
Comment from content spinner
Time: May 20, 2013, 15:23
Thanks for ones marvelous posting! I certainly enjoyed reading it, you
will be a great author.I will be sure to bookmark your blog and will come back someday.
I want to encourage you continue your great writing, have a nice evening!
Comment from Lucy
Time: May 21, 2013, 3:13
Hey just wanted to give you a quick heads up and let you know a few of the pictures aren’t loading properly. I’m not sure why but I
think its a linking issue. I’ve tried it in two different internet browsers and both show the same outcome.
Write a comment