Groups | Blog | Home
all groups > dotnet xml > june 2005 >

dotnet xml : XmlSerializer, XmlAnyElement and CDATA


Munish
6/13/2005 11:10:03 AM
(VS.NET 2K3, C#)
Hi,
I need to serialize/deserialize an element using XmlSerializer that can
contain any valid XML including a CDATA section.

[XmlRootAttribute]
public class Test
{
[XmlAnyElement]
public XmlElement Anything;
}

This doesn't work with an XML like the following:
<Test><![CDATA[ msgbox "Hi222"]]></Test>

However this works fine:
<Test><Wrap><![CDATA[ msgbox "Hi222"]]></Wrap></Test>

Unfortunately in my case the XML between <Test> comes from outside of my
application and can contain virtually anything. I just need to be able to
read/write this data and may be pass on to someone else. Changing XmlElement
in the example above to XmlLinkedNode doesn't help.

Zafar Abbas
6/13/2005 12:07:11 PM
The object model to deserialize unwrapped [CDATA] nodes should be different:
you need to use XmlNode (or XmlNode[]) instead of XmlElement

[XmlRootAttribute]

public class Test

{

[XmlAnyElement]

public XmlNode Anything;

}



[quoted text, click to view]

Munish
6/13/2005 2:16:03 PM
Thanks for replying, but that doesn't work. As I mentioned before going to a
common base class of XmlElement and XmlCDataSection (XmlLinkedNode for
example) doesn't help.

Any other way?
AddThis Social Bookmark Button