471,595 Members | 972 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,595 software developers and data experts.

XmlSerializer, XmlAnyElement and CDATA

(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.

Thanks for your help.
Nov 12 '05 #1
2 9316
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;

}

"Munish" <Mu****@discussions.microsoft.com> wrote in message
news:B6**********************************@microsof t.com...
(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.

Thanks for your help.

Nov 12 '05 #2
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?

Nov 12 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Cade Perkins | last post: by
3 posts views Thread by Cindy Liu | last post: by
4 posts views Thread by Steve Long | last post: by
12 posts views Thread by Peter Michaux | last post: by
reply views Thread by Anwar ali | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.