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

dotnet xml : How to detect, if an element string has to be written as CDATA


Stefan Gentzmer
12/2/2005 10:56:36 PM
Hello,

how can i detect, if the text for a XmlElement has to be written as CDATA (i
am using the XmlWriter) instead of a normal string.

For example

<start>
<node1>HelloWorld</node1>
<node2>a>b</node2>
</start>

How can i programmatically detect, that node1 can be written by using
XmlWriter.WriteString and node2 must be written by XmlWriter.WriteCData




Stefan Gentzmer
12/3/2005 12:00:00 AM
[quoted text, click to view]

Sorry, that was not my question.

I'm writing an object modell to xml via XmlWriter.

So, there is no XmlElement-Object.

The only thing that a have is the XmlWriter and a string-field with a value
e.g."Test1" or "a > b".

Now, i need a possibility to decide,
if i had to use XmLWriter.WriteString
or XmlWriter.WriteCData to write this value to the Xml-File.

Pascal Schmitt
12/3/2005 1:08:15 AM
Hello!

[quoted text, click to view]

There are multiple types that derive from System.Xml.XmlCharacterData -
maybe you can test the type of your XmlNode:

XmlNode n = /* get node */...;

if( n is XmlCDataSection ){ /* <![CDATA[ ... ]]> */ }
else if( n is XmlText ){ /* plain text */ }
else if( n is XmlComment ){ ... }
else if( n is XmlSignificantWhitespace ){ ... }
else if( n is XmlWhitespace ){ ... }
else { /* no text node */ }


--
Peter Flynn
12/3/2005 1:29:26 PM
[quoted text, click to view]

If the character data contains & or < that must not be interpreted as
markup, then it must be enclosed in a CDATA section.

The example "a>b" does not, so it does not need to be. The > sign is
only interpreted as markup once a < sign has been encountered.

See http://xml.silmaril.ie/authors/cdata/

///Peter
Pascal Schmitt
12/3/2005 4:06:57 PM
Hello!

[quoted text, click to view]

Ah - ok. You don't have to decide this - you can always use WriteString,
the XmlTextWriter will do the escaping of &,< and > for you.


--
AddThis Social Bookmark Button