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.Serializati on.IXmlSerializ able.
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
<LanguageString s>
<LanguageStri ng Language="" TranslationStat us="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.Sche ma.XmlSchema Implements System.Xml.Seri alization.IXmlS erializable.Get Schema
Return Nothing
End Function
Public Sub ReadXml(ByVal reader As System.Xml.XmlR eader) Implements System.Xml.Seri alization.IXmlS erializable.Rea dXml
reader.ReadStar tElement()
reader.MoveToCo ntent()
reader.ReadStar tElement()
reader.MoveToCo ntent()
Me.arrLanguageS trings.Clear()
Dim lang As String = ""
Dim ts As Object = Nothing
While reader.NodeType <> XmlNodeType.End Element
If reader.HasAttri butes() Then
Try
lang = reader.GetAttri bute("Language" )
ts = reader.GetAttri bute("Translati onStatus")
Catch ex As Exception
Debug.WriteLine ("Error reading attributes:" + ex.ToString)
End Try
Else
lang = ""
End If
Dim Value As String = reader.ReadInne rXml
Me.Item(lang) = Value
If Not ts Is Nothing Then
SetTranslationS tatus(lang, _
CType(System.En um.Parse(GetTyp e(LanguageStrin gObject.enumTra nslationstatus) , ts.ToString), _
LanguageStringO bject.enumTrans lationstatus))
End If
reader.MoveToCo ntent()
End While
'
If Me.arrLanguageS trings.Count > 0 Then
reader.MoveToCo ntent()
reader.ReadEndE lement()
End If
reader.MoveToCo ntent()
reader.ReadEndE lement()
End Sub
Public Sub WriteXml(ByVal writer As System.Xml.XmlW riter) Implements System.Xml.Seri alization.IXmlS erializable.Wri teXml
writer.WriteSta rtElement("Lang uageStrings")
For Each lso As LanguageStringO bject In arrLanguageStri ngs
writer.WriteRaw ("<LanguageStri ng Language=""" + lso.Lang + """ TranslationStat us=""" + lso.translation status.ToString + """>" _
+ lso.Text _
+ "</LanguageString> ")
Next
writer.WriteEnd Element()
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".