XML Serialization of HTML

csharpnoob picture csharpnoob · Oct 1, 2009 · Viewed 11.5k times · Source

Okay this one DID it! Thanks to all of you!

public class Result
{
    public String htmlEscaped
    {
        set;
        get;
    }

    [XmlIgnore]
    public String htmlValue
    { set; get; }

    [XmlElement("htmlValue")]
    public XmlCDataSection htmlValueCData
    {
        get
        {
            XmlDocument _dummyDoc = new XmlDocument();
            return _dummyDoc.CreateCDataSection(htmlValue);
        }
        set { htmlValue = (value != null) ? value.Data : null; }
    }
}

    Result r = new Result();
    r.htmlValue = ("<b>Hello</b>");
    r.htmlEscaped = ("<b>Hello</b>");
    XmlSerializer xml = new XmlSerializer(r.GetType());
    TextWriter file = new StreamWriter(Environment.CurrentDirectory + "\\results\\result.xml", false, System.Text.Encoding.Default);
    xml.Serialize(file, r);
    file.Close();

RESULT:

<?xml version="1.0" encoding="Windows-1252"?>
<Result xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <htmlEscaped>&lt;b&gt;Hello&lt;/b&gt;</htmlEscaped>
  <htmlValue><![CDATA[<b>Hello</b>]]></htmlValue>
</Result>

As you can see, after CDATA is return type, no more escaped html in XML file on filesystem. The JSON Serialization isn't working anymore, but this can be fixed with a little type extention.


QUESTION WAS:

Maybe someone knows how to make do it...

I have this Class:

public class Result
{
    public String htmlValue
    {
        get;
        set;
    }
}

I use this to serialize it to XML

Result res = new Result();
res.htmlValue = "<p>Hello World</p>";
XmlSerializer s = new XmlSerializer(res.GetType());
TextWriter w = new StreamWriter(Environment.CurrentDirectory + "\\result.xml", false, System.Text.Encoding.Default);
s.Serialize(w, res);
w.Close();

Works fine i get this:

<?xml version="1.0" encoding="Windows-1252"?>
<Result xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <htmlValue>&lt;b&gt;Hello World&lt;/b&gt;</htmlValue>
</Result>

What can do i have to change to get this:

<?xml version="1.0" encoding="Windows-1252"?>
<Result xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <htmlValue><![CDATA[<b>Hello World</b>]]></htmlValue>
</Result>

I've already searched but I can't find anything. The type of htmlValue have to stay String, because of other Serialisations JSON, etc.

Tricky one... Thanks in advance for suggestions

  • HTML is correct in String within C#. Why decode or encode?
  • XmlSerializer saved the HTML escaped to XML file.
  • Don't use C# for consuming.

Is external tool which accept this:

<htmlValue><![CDATA[<b>Hello World</b>]]></htmlValue>

but not

<htmlValue>&lt;b&gt;Hello World&lt;/b&gt;</htmlValue>

I do the same with JSON Serializer, in file on hard drive the HTML is saved correct. Why and where to use HTTP Utility to prevent that? And how to get <![CDATA[ ]]> around it.

Can you give a code sample? Are there any other Serializer than the C# own one?

I've found this Link .NET XML Serialization of CDATA ATTRIBUTE from Marco André Silva, which does I need to do, but it's different, how to include this without changing Types?

Answer

Thomas Levesque picture Thomas Levesque · Oct 2, 2009

Here's a simple trick to do achieve what you want. You just need to serialize a XmlCDataSection property instead of the string property :

(it's almost the same as John's suggestion, but a bit simpler...)

public class Result
{
    [XmlIgnore]
    public String htmlValue
    {
        get;
        set;
    }

    private static XmlDocument _dummyDoc;

    [XmlElement("htmlValue")]
    public XmlCDataSection htmlValueCData
    {
        get { return _dummyDoc.CreateCDataSection(htmlValue); }
        set { htmlValue = (value != null) ? value.Data : null; }
    }
}