XSD2Code - Output elements of type string as cdata

Sep 18, 2009 at 1:24 PM

Hi,

Has anybody managed to serialize and deserialize xml using cdata tags. Ideally I would like an option in xsd2code to serialize all string elements as cdata

 

I have the following which essentially works but as the type is still string and not xmlCdataSection it is being escaped on serialzation.

[System.Xml.Serialization.XmlTextAttribute()]
        public string Value
        {
            get
            {
                
                XmlDocument doc = new XmlDocument();
                return doc.CreateCDataSection(this.valueField).OuterXml.ToString();
            }
            set
            {
                if ((this.valueField != null))
                {
                    if ((valueField.Equals(value) != true))
                    {
                        this.valueField = value.ToString();
                        this.OnPropertyChanged("Value");
                    }
                }
                else
                {
                    this.valueField = value.ToString();
                    this.OnPropertyChanged("Value");
                }
            }
        }

Regards,

Phil

Nov 4, 2009 at 12:50 PM

Isn't this a hack to work around the fact that XmlSerializer doesnt do CDATA? Should Xsd2Code implement hacks? What is the problem with using escaped data? Does your reader at the other end not perform unescaping?

Another option could be:

Xsd2Code should be able to be given a list of members NOT to generate code for, and then you put the relevant code in the partial class file.. i.e. put your hack in the partial, so that regeneration does not overwrite it

Jan 21, 2010 at 3:12 PM
Edited Jan 21, 2010 at 3:14 PM

You should set de datatype op your property to XmlCDataSection

To avoid the propertyname is shown use the [System.Xml.Serialization.XmlAnyElementAttribute()]

May 17 at 11:29 AM
cjard wrote:
Isn't this a hack to work around the fact that XmlSerializer doesnt do CDATA? Should Xsd2Code implement hacks? What is the problem with using escaped data? Does your reader at the other end not perform unescaping? Another option could be: Xsd2Code should be able to be given a list of members NOT to generate code for, and then you put the relevant code in the partial class file.. i.e. put your hack in the partial, so that regeneration does not overwrite it
Unfortunately partial class does not resolve problems, because change order of markups and not always we can do this. Besides e.g. I have a client who has system with doesn't support escaped data and I have to adjust.... ;/