0

I am trying to serialise the following class using xmlserializer and one of the property is bound to contain some html data.

[System.SerializableAttribute()]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class Data 
{ 
  private string dataWithHTMLContent;
  [System.Xml.Serialization.XmlElementAttribute(Order =1)]
 public string DataWithHTMLContent
 {
    get
   {
       return this.dataWithHTMLContent;
   }
   set
   {
       this.dataWithHTMLContent = value;
   }
}
}    

//serializes Object and saves to file system     
var data= new Data();
data.DataWithHTMLContent= "Some data A & B with value <=10";
var xmlSerializer = new XmlSerializer(typeof(Data));
var sw = new StringWriter();
xmlSerializer.Serialize(sw, data);
var xmlDoc = new XmlDocument();
var xmlString = sw.ToString();
xmlDoc.LoadXml(xmlString);  
xmlDoc.Save...//to save to file system for checking

When i created the xml file the contents of the DataWithHTMLContent node gets html encoded (This content is not entirely HTML in nature, it contains certain reserved characters). How can i stop the html encoding operation. Is there any particular attribute that can be applied to DataWithHTMLContent property to avoid getting html encoded.

I do not want my data to show as "Some data A &amp; B with value &lt;=10"

Lin
  • 633
  • 8
  • 26
  • This is dangerous because some elements in HTML do not need a closing tag (ex:
    ). It's only safe to do your solution if it's XHTML.
    – LostInComputer May 21 '14 at 00:53
  • the data i was trying to store is not entirely html in nature i will edit the question – Lin May 21 '14 at 01:05
  • By design, you cannot (mis)use XmlSerializer to output malformed XML. Why do you want to output unescaped `&` and `<` characters? No XML-compliant parser will be able to read the resulting file. – Michael Liu May 21 '14 at 02:42
  • Thanks. looks like i need to wrap it up as CDATA as suggested by Erik – Lin May 21 '14 at 03:20

0 Answers0