all groups > dotnet xml > march 2004 >
You're in the

dotnet xml

group:

Serializing as CData?


Serializing as CData? Paul Hatcher
3/31/2004 4:39:36 PM
dotnet xml:
Is is possible to tell the XML serializer to output an element's content in
a CDATA wrapper?

Paul

Re: Serializing as CData? Paul Hatcher
3/31/2004 5:58:23 PM
Could you explain why? The element concerned will contain arbitrary content
(it's source code) and I want to preserve the whitespace that is present - I
tried just storing it as a string but for example, CrLf is converted to Lf.

Paul

[quoted text, click to view]

Re: Serializing as CData? Dimitre Novatchev [MVP XML]
3/31/2004 6:47:58 PM

[quoted text, click to view]


This has been shown by many people to be a bad idea.


Cheers,

Dimitre Novatchev [XML MVP],
FXSL developer, XML Insider,

http://fxsl.sourceforge.net/ -- the home of FXSL
Resume: http://fxsl.sf.net/DNovatchev/Resume/Res.html

Re: Serializing as CData? Dimitre Novatchev [MVP XML]
3/31/2004 8:30:02 PM

[quoted text, click to view]
Lf.

Very briefly, because in this way all the structure/types of the document is
destroyed to linear text, and this is highly irreversible.

See e.g. the explanation of Norman Walsh in his xml.com article "Escaped
Markup Considered Harmful"

at:
http://www.xml.com/pub/a/2003/08/20/embedded.html


Cheers,

Dimitre Novatchev [XML MVP],
FXSL developer, XML Insider,

http://fxsl.sourceforge.net/ -- the home of FXSL
Resume: http://fxsl.sf.net/DNovatchev/Resume/Res.html

Re: Serializing as CData? Christoph Schittko [MVP]
3/31/2004 9:08:01 PM
Paul,

It's actually pretty intuitive ;)
A field of type System.Xml.XmlCDataSection will serialize as a CDATA section
.... as with all types derived from XmlNode, the XmSerializer just writes out
the content of the field without enclosing tags if you attach an
XmlAnyElementAttribute to the field [0].

--
HTH
Christoph Schittko [MVP]
Software Architect, .NET Mentor

[0] http://www.topxml.com/xmlserializer/serializing_xml_nodes.asp

[quoted text, click to view]

Re: Serializing as CData? Paul Hatcher
3/31/2004 10:01:06 PM
Dimitre

Following the link from that article you to Norman Walsh's blog, you get...

....Finally, he asks what my problem is given that CDATA is part of XML. I
don't have any problem with CDATA. CDATA sections are fine, as long as what
you're putting in them is text. What's evil about this escaped markup
nonsense is the semantics provided by RSS/Atom, not the use of CDATA to
avoid lots of ampersands and semicolons. That's my problem....

My point is that what I have *is* text, not marked up XML - so can you give
me an answer now :-)

Alternatively, can you suggest another mechanism for preserving whitespace
in the value?

Paul

[quoted text, click to view]

Re: Serializing as CData? Dimitre Novatchev [MVP XML]
4/1/2004 7:20:13 AM

[quoted text, click to view]

Sorry Paul,

You were asking how "to output an element's content in a CDATA wrapper". I
thought that by content you mean the xml fragment consisting of all of this
element's children nodes.

Of course, if the element has just a single text node child, then putting
this text into a CDATA section is perfectly OK.

Christoph already provided an answer how to do this in .Net.

Another, platform-independent way of generating a CDATA section is using
XSLT and the cdata-section-elements attribute of xsl:output.


Cheers,

Dimitre Novatchev [XML MVP],
FXSL developer, XML Insider,

http://fxsl.sourceforge.net/ -- the home of FXSL
Resume: http://fxsl.sf.net/DNovatchev/Resume/Res.html

Re: Serializing as CData? Paul Hatcher
4/1/2004 10:23:40 AM
Christoph

Thanks for the response, my problem is that the solution would embed XML
into the domain object - which is not what I want. Currently I have a
property defined as follows...

<System.Xml.Serialization.XmlElementAttribute(ElementName:="source")> _
Public Property Source() As String
Get
Return _source
End Get
Set (Value As String)
_source = Value
End Set
End Property

All I want is that when it serializes that it is wrappered in CDATA as I
can't control the contents and I can't find a way of telling the serializer
to do this. If I understand your suggestion I would declare the property as

<System.Xml.Serialization.XmlAnyElementAttribute(Name:="source")> _
Public Property Source() As XmlCDataSection
Get
Return _source
End Get
Set (Value As XmlCDataSection)
_source = Value
End Set
End Property

Do I have to implement ISerializable to get the level of control I need?

Paul

"Christoph Schittko [MVP]" <christophdotnetINVALID@austin.rr.com> wrote in
message news:e54DUa5FEHA.2876@TK2MSFTNGP09.phx.gbl...
[quoted text, click to view]

Re: Serializing as CData? Paul Hatcher
4/1/2004 10:25:49 AM
Dimitre

No problem - have a look at my reply to Christoph as I don't believe his
solution will work for me.

Paul

[quoted text, click to view]

Re: Serializing as CData? Christoph Schittko [MVP]
4/1/2004 11:16:38 PM
Paul,

Yes you did understand my suggestion. Can you clarify why you believe that
it will not work for you?

You can implement IXmlSerializable, but that means you are in charge of
serializing and deserializing the entire object, not just the single
property. Note that IXmlSerializable is not officially supported until the
Whidbey release [0].

--
HTH
Christoph Schittko [MVP]
Software Architect, .NET Mentor

[0] http://weblogs.asp.net/cschittko/archive/2003/10/28/34313.aspx

[quoted text, click to view]

Re: Serializing as CData? Paul Hatcher
4/2/2004 7:01:36 PM
Christoph

I did manage to get it working, but it's a bit non-inituitive. Basically the
problem is that I don't have or want to have an XML document for my domain
objects - XML is just a persistence mechanism and 99% of the application
won't be aware of it.

Problem is that XmlCDataSection can only be created by a XmlDocument, so I
have to create a fake document so that I can expose the CDATA section to be
persisted. Here's the eventual code...

<System.Xml.Serialization.XmlIgnoreAttribute()> _
Public Property Source() As String
Get
Return _source
End Get
Set(ByVal Value As String)
_source = Value
End Set
End Property

<System.Xml.Serialization.XmlAnyElementAttribute(Name:="source") _
Public Property XmlSource() As Xml.XmlElement
Get
Dim doc As New Xml.XmlDocument
Dim elem As Xml.XmlElement = doc.CreateElement("source")
Dim cdata As Xml.XmlCDataSection =
doc.CreateCDataSection(Source)
elem.AppendChild(cdata)

Return elem
End Get
Set(ByVal Value As Xml.XmlElement)
Source = Value.InnerText.Replace(vbLf, vbCrLf)
End Set
End Property

Funnily enough, I still have a problem with CrLf -> Lf on serialization, but
it's easily handled as you can see. I had to create the surrounding element
as otherwise I get a naked CDATA section in the document, even with the
XmlAnyElementAttribute declaration being present.

I think I'll write this up for one of the tip sites as it's taken me a while
to locate the solution and I'm sure it will be useful to others; be much
nicer if there was a System.Xml.Serialization.XmlCDataElementAttribute
though :-)

Paul


"Christoph Schittko [MVP]" <christophdotnetINVALID@austin.rr.com> wrote in
message news:esWHzGHGEHA.2600@TK2MSFTNGP12.phx.gbl...
[quoted text, click to view]

Re: Serializing as CData? Christoph Schittko [MVP]
4/6/2004 12:12:18 AM
I agree, it's not very intuitive ... and that you have to use the
XmlDocument as a "factory" is pretty much true for all types derived from
XmlNode. It does make some sense if you are creating an XmlDocument, but it
makes no sense whatsoever in the asmx Web service scenario. I did show that
in the article on topxml, but it would hurt if that information is available
in more places.

I'm glad you got it working ...
--

Christoph Schittko [MVP]
Software Architect, .NET Mentor


[quoted text, click to view]
AddThis Social Bookmark Button