Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML Serialization - XmlCDataSection as Serialization.XmlText

I am having problems serializing a cdata section using c#

I need to serialize XmlCDataSection object property as the innertext of the element.

The result I am looking for is this:

<Test value2="Another Test">
  <![CDATA[<p>hello world</p>]]>
</Test>

To produce this, I am using this object:

public class Test
{
    [System.Xml.Serialization.XmlText()]
    public XmlCDataSection value { get; set; }

    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string value2 { get; set; }
}

When using the xmltext annotation on the value property the following error is thrown.

System.InvalidOperationException: There was an error reflecting property 'value'. ---> System.InvalidOperationException: Cannot serialize member 'value' of type System.Xml.XmlCDataSection. XmlAttribute/XmlText cannot be used to encode complex types

If I comment out the annotation, the serialization will work but the cdata section is placed into a value element which is no good for what I am trying to do:

<Test value2="Another Test">
  <value><![CDATA[<p>hello world</p>]]></value>
</Test>

Can anybody point me in the right direction to getting this to work.

Thanks, Adam

like image 408
Adam Jenkin Avatar asked Sep 09 '09 10:09

Adam Jenkin


People also ask

What is System XML serialization XmlElementAttribute?

XmlElementAttribute(Type) Initializes a new instance of the XmlElementAttribute class and specifies a type for the member to which the XmlElementAttribute is applied. This type is used by the XmlSerializer when serializing or deserializing object that contains it.

What is System XML serialization?

XmlSerializerNamespaces Class (System. Xml. Serialization) Contains the XML namespaces and prefixes that the XmlSerializer uses to generate qualified names in an XML-document instance.

What is CDATA in XML example?

A CDATA section is used to mark a section of an XML document, so that the XML parser interprets it only as character data, and not as markup. It comes handy when one XML data need to be embedded within another XML document.

How do I add CDATA to XML?

CDATA sections can appear inside element content and allow < and & character literals to appear. A CDATA section begins with the character sequence <! [CDATA[ and ends with the character sequence ]]>. Between the two character sequences, an XML processor ignores all markup characters such as <, >, and &.


1 Answers

Thanks Richard, only now had chance to get back to this. I think I have resolved the problem by using your suggestion. I have created a CDataField object using the following:

public class CDataField : IXmlSerializable
    {
        private string elementName;
        private string elementValue;

        public CDataField(string elementName, string elementValue)
        {
            this.elementName = elementName;
            this.elementValue = elementValue;
        }

        public XmlSchema GetSchema()
        {
            return null;
        }

        public void WriteXml(XmlWriter w)
        {
            w.WriteStartElement(this.elementName);
            w.WriteCData(this.elementValue);
            w.WriteEndElement();
        }

        public void ReadXml(XmlReader r)
        {                      
            throw new NotImplementedException("This method has not been implemented");
        }
    }
like image 181
Adam Jenkin Avatar answered Nov 08 '22 05:11

Adam Jenkin