I agree, it's not very intuitive ... and that you have to use the
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
I'm glad you got it working ...
"Paul Hatcher" <phatcher@spamless.cix.co.uk> wrote in message
news:%23NmdMyNGEHA.2732@tk2msftngp13.phx.gbl...
> 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...
> > 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 > >
> > "Paul Hatcher" <phatcher@spamless.cix.co.uk> wrote in message
> > news:OvWsGs8FEHA.2664@TK2MSFTNGP11.phx.gbl...
> > > 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...
> > > > 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 > > > >
> > > > "Paul Hatcher" <phatcher@spamless.cix.co.uk> wrote in message
> > > > news:%23ZhQMN2FEHA.1128@TK2MSFTNGP11.phx.gbl...
> > > > > 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
> > > > >
> > > > > "Dimitre Novatchev [MVP XML]" <dnovatchev@yahoo.com> wrote in
> message
> > > > > news:c4f20a$2gpp9u$1@ID-152440.news.uni-berlin.de...
> > > > > >
> > > > > > "Paul Hatcher" <phatcher@spamless.cix.co.uk> wrote in message
> > > > > > news:OuzSiF0FEHA.1156@TK2MSFTNGP12.phx.gbl...
> > > > > > > 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.