This doesn't make sense in an XMLDocument, as it's not possible to have a
node value that contains tags (as these would be node type tag), but there
are places where CDATA is not going to work because you want to perform
transformations on the XML file and your code contains tags in strings.
However, it does make sense if you want to generate XML from an object
tree, and have decided to code your own nodes as strings that may contain
mixed XML data.
One example might be to allow a node that may contain a subset of HTML that
you have defined in a schema as a mixed type node.
If you encode this as a string then it will always be escaped by the
XMLSerializer, but you can get around this by implementing interface
Xml.Serialization.IXmlSerializable.
If the XML Serializer detects this then it calls your implementation
instead of its own.
Here's an example in VB.Net I use to implement a collection of language
specific elements such as
<LanguageStrings>
<LanguageString Language="" TranslationStatus="Changed">
Are you <B>certain</B> that you want to do <U>this</U> with your XML?
</LanguageString>
</LanguageStrings>
As you can see, there are tagged values in the string that we want to
remain as valid XML so we can transform them to other formats at render
time, and we've already validated them and they are correct for the schema.
Here's the code:
Public Function GetSchema() As System.Xml.Schema.XmlSchema Implements System.Xml.Serialization.IXmlSerializable.GetSchem a
Return Nothing
End Function
Public Sub ReadXml(ByVal reader As System.Xml.XmlReader) Implements System.Xml.Serialization.IXmlSerializable.ReadXml
reader.ReadStartElement()
reader.MoveToContent()
reader.ReadStartElement()
reader.MoveToContent()
Me.arrLanguageStrings.Clear()
Dim lang As String = ""
Dim ts As Object = Nothing
While reader.NodeType <> XmlNodeType.EndElement
If reader.HasAttributes() Then
Try
lang = reader.GetAttribute("Language")
ts = reader.GetAttribute("TranslationStatus")
Catch ex As Exception
Debug.WriteLine("Error reading attributes:" + ex.ToString)
End Try
Else
lang = ""
End If
Dim Value As String = reader.ReadInnerXml
Me.Item(lang) = Value
If Not ts Is Nothing Then
SetTranslationStatus(lang, _
CType(System.Enum.Parse(GetType(LanguageStringObje ct.enumTranslationstatus), ts.ToString), _
LanguageStringObject.enumTranslationstatus))
End If
reader.MoveToContent()
End While
'
If Me.arrLanguageStrings.Count > 0 Then
reader.MoveToContent()
reader.ReadEndElement()
End If
reader.MoveToContent()
reader.ReadEndElement()
End Sub
Public Sub WriteXml(ByVal writer As System.Xml.XmlWriter) Implements System.Xml.Serialization.IXmlSerializable.WriteXml
writer.WriteStartElement("LanguageStrings")
For Each lso As LanguageStringObject In arrLanguageStrings
writer.WriteRaw("<LanguageString Language=""" + lso.Lang + """ TranslationStatus=""" + lso.translationstatus.ToString + """>" _
+ lso.Text _
+ "</LanguageString>")
Next
writer.WriteEndElement()
End Sub
On Sat, 08 Oct 2005 02:46:21 +0200, Pascal Schmitt wrote:
Hello!
I have nodes in the XmlDocument I create that need to be written exactly as
is - not converting < to %lt; and no changing the whitespace. (This is for
the SpreadsheetML schema and it reads the <Data> nodes literally.)
AFAIK that's not possible with XmlDocument. But XmlTextWriter has a
WriteRaw-Method.
Is it really not possible to use < or <[CDATA[..]]>?
For <x>5<7</x> a parser will see a text node containing "5<7".