I'm trying to dynamically generate an HTML table by using XmlSerializer and a set of classes like this:
[XmlInclude(typeof(Th))]
public class Td
{
[XmlElement("span")]
public string Designation { get; set; }
[XmlAttribute("colspan")]
public int ColSpan { get; set; }
[XmlAttribute("rowspan")]
public int RowSpan { get; set; }
public Td(string designation, int colspan)
{
Designation = designation;
ColSpan = colspan;
RowSpan = 1;
}
public Td()
{
}
}
The problem here is that the Designation property can have a tag as value like <option...>, so when I serialize my model I get <option...> instead of <option...>
I can solve the problem by using the string.Replace method like this: Replace("<", "<").Replace(">", ">");
Is there a clean way to get the expected result without the use of string.Replace?
You can create another property that exposes Designation as an XmlNode:
[XmlIgnore]
public string Designation { get; set; }
[XmlElement("span")]
public XmlNode DesignationAsXml
{
get
{
XmlDocument doc = new XmlDocument();
doc.InnerXml = "<root>" + this.Designation + "</root>";
return doc.DocumentElement.FirstChild;
}
set
{
throw new NotSupportedException();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With