7

I have a problem with CDATA deserialization using standard .Net XmlSerializer.

Update: I get XML from external system and I can't influence it's format so I can't make CData be enclosed in a separate Element of Attribute.

Serialization gives this:

<?xml version="1.0" encoding="utf-16"?>
<MyClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><![CDATA[Hello, world!]]></MyClass>

Deserialization does not restore object to it's original state.

Here's class that is being serialized:

public class MyClass
{
    string _data;

    [XmlIgnore]
    public string Data
    {
        get { return _data; }
        set { _data = value; }
    }

    [XmlAnyElement]
    public XmlCDataSection CData
    {
        get { return new XmlDataDocument().CreateCDataSection(Data); }
        set { Data = value.Value; }
    }
}

Here's the test which fails:

[Test]
public void CData_as_inner_text_test()
{
    MyClass item = new MyClass();

    item.Data = "Hello, world!";

    XmlSerializer serializer = new XmlSerializer(item.GetType());
    string serialized;

    using (StringWriter sw = new StringWriter())
    {
        serializer.Serialize(sw, item);
        serialized = sw.GetStringBuilder().ToString();
    }

    MyClass deserialized;

    using (StringReader sr = new StringReader(serialized))
    {
        deserialized = (MyClass)serializer.Deserialize(sr);
    }

    Assert.AreEqual(item.Data, deserialized.Data); // For some reason, deserialized.Data == null
}

I found the same problem here but there's no answer: XmlSerializer, XmlAnyElement and CDATA

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Konstantin Spirin
  • 20,609
  • 15
  • 72
  • 90

2 Answers2

11

The CData property ends up null, because the content of the CDATA section ends up in the Data property, where it is being ignored...

<MyClass><![CDATA[Hello, world!]]></MyClass>

is absolutely equivalent to:

<MyClass>Hello, world!</MyClass>

You shouldn't care whether the external app writes the content of MyClass as CData or not. Likewise, the external app shouldn't care how you write it out.

IOW, this should be all you need:

public class MyClass
{
    string _data;

    [XmlText]
    public string Data
    {
        get { return _data; }
        set { _data = value; }
    }
}
oefe
  • 19,298
  • 7
  • 47
  • 66
  • Thanks! It appeared I didn't truly realize what CDATA does. Parser just ignores it and treats it's contents as non-formatted text. There's one minor bug in your answer. My xml is absolutely equivalent to Hello, world! (no '<' and '>' around "Hello, world!") – Konstantin Spirin Dec 30 '08 at 02:16
  • 1
    Why are you assuming the inner text is *Hello, world!*? What if it actually needs to be CDATA? – user247702 May 27 '11 at 10:27
  • 1
    @Stijn: I don't make any such assumption. It never _needs_ to be CDATA , at least from a technical point of view. CDATA is just a convenience for human readers / writers. – oefe May 28 '11 at 08:52
  • @Stijn If it *needs* to be CDATA (as in, the data contains CDATA), you need to encode the CDATA properly (which .NET's serializers *will* do). If you need it to be CDATA because you've got a stupid parser, you need to change your parser :) CDATA is just a way of encoding a string. The data for `<![CDATA[Hello, world!]]>` is exactly the same as for `Hello, world!` - if you rely on the CDATA there in any way, you're reading XML all wrong :) – Luaan Jul 30 '14 at 07:27
0

First declare a property as XmlCDataSection

public XmlCDataSection ProjectXml { get; set; }

in this case projectXml is a string xml

ProjectXml = new XmlDocument().CreateCDataSection(projectXml);

when you serialize your message you will have your nice format (notice )

<?xml version="1.0" encoding="utf-16"?>
<MessageBase xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:type="Message_ProjectStatusChanged">
  <ID>131</ID>
  <HandlerName>Plugin</HandlerName>
  <NumRetries>0</NumRetries>
  <TriggerXml><![CDATA[<?xml version="1.0" encoding="utf-8"?><TmData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Version="9.0.0" Date="2012-01-31T15:46:02.6003105" Format="1" AppVersion="10.2.0" Culture="en-US" UserID="0" UserRole=""><PROJECT></PROJECT></TmData>]]></TriggerXml>
  <MessageCreatedDate>2012-01-31T20:28:52.4843092Z</MessageCreatedDate>
  <MessageStatus>0</MessageStatus>
  <ProjectId>0</ProjectId>
  <UserGUID>8CDF581E44F54E8BAD60A4FAA8418070</UserGUID>
  <ProjectGUID>5E82456F42DC46DEBA07F114F647E969</ProjectGUID>
  <PriorStatus>0</PriorStatus>
  <NewStatus>3</NewStatus>
  <ActionDate>0001-01-01T00:00:00</ActionDate>
</MessageBase>
Sebastian Castaldi
  • 8,580
  • 3
  • 32
  • 24