Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML Serialization of HTML

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?

like image 507
csharpnoob Avatar asked Oct 01 '09 21:10

csharpnoob


People also ask

What is XML serialization?

XML serialization is the process of converting XML data from its representation in the XQuery and XPath data model, which is the hierarchical format it has in a Db2® database, to the serialized string format that it has in an application.

Is XML a serialization format?

XML. XML is a human-readable serialization protocol. A well known XML-like format is HTML which is used to determine the structure of web pages.

What is serializing and deserializing XML?

Serialization is a process by which an object's state is transformed in some serial data format, such as XML or binary format. Deserialization, on the other hand, is used to convert the byte of data, such as XML or binary data, to object type.

Is a Namespsace for XML serialization?

The central class in the namespace is the XmlSerializer class. To use this class, use the XmlSerializer constructor to create an instance of the class using the type of the object to serialize. Once an XmlSerializer is created, create an instance of the object to serialize.


1 Answers

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; }
    }
}
like image 107
Thomas Levesque Avatar answered Oct 17 '22 12:10

Thomas Levesque