The following class (from an xml) has several attributes and contains text.
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "xxx")]
public partial class consultaSelect : EntityBase<consultaSelect>
{
private string scriptField;
[System.Xml.Serialization.XmlAttributeAttribute()]
public string script
{
get
{
return this.scriptField;
}
set
{
if ((this.scriptField != null))
{
if ((scriptField.Equals(value) != true))
{
this.scriptField = value;
this.OnPropertyChanged("script");
}
}
else
{
this.scriptField = value;
this.OnPropertyChanged("script");
}
}
}
private string valueField;
[System.Xml.Serialization.XmlTextAttribute()]
public string Value
{
get
{
return this.valueField;
}
set
{
if ((this.valueField != null))
{
if ((valueField.Equals(value) != true))
{
this.valueField = value;
this.OnPropertyChanged("Value");
}
}
else
{
this.valueField = value;
this.OnPropertyChanged("Value");
}
}
}
}
The problem is with value. I want it to be a CDATA. However, when writing the get as follows:
get
{
XmlDocument doc = new XmlDocument();
return doc.CreateCDataSection(valueField);
}
this in theory returns the value as CDATA, but it forces me to decorate it with a tag [XmlElement("CDataElement")]
that is not what I want, since I want it to be part of the original. When I decorate it that way, it generates one more element in the final XML:
<select script="pepe">
<CDataElement><![CDATA[SELECT * FROM Users WHERE UserId > 10]]></CDataElement>
</select>
and what I want is:
<select script="pepe">
<![CDATA[SELECT * FROM Users WHERE UserId > 10]]>
</select>
So the problem is, should I decorate it in another way, or am I totally wrong and I have to do it in another way?
I hope these modifications serve you. Add the following in your class
As a reference, the solution proposed by Sergio is totally correct. However in my case the XML is much more complex and that is why I needed to do it with automatic decorators. To avoid that, I did the following (although it is not recommended):
Add the following in the class:
and also decorate the Value element like
After this, all I did was add a procedure, after deserializing the XML, that would read the CDATA elements I wanted by hand and store them in the class: