Hi
I am trying to post-process some XML-documents from some third-party
software.
I open them, find the right element, manipulate the text and write it back
to the element.
Dim x As New Xml.XmlDocument
Dim nl As Xml.XmlNodeList
Dim text As String
Dim textOutput As String
x.Load(filename)
nl = x.GetElementsByTagName("text")
[snip, some manipulation of the text, not important...]
nl.Item(0).InnerText = "<![CDATA[ " & textOutput & " ]]>"
'nl.Item(0).InnerText = textOutput
x.Save("C:\Temp\XmlYt.xml")
Now, the data I write back is encoded and not enclosed in CDATA. I tried
addding the CDATA-myself, but that just gets encoded as well.
Before:
<text><![CDATA[<block><p x1="294" y1="602" x2="795" y2="1105">HORSENS - I
morgen kan være AaB's sidste dag som tophold i superligaen i denne
sæson.</p><p x1="294" y1="602" x2="795" y2="1105">AC Horsens kan sende AaB
ned fra førstepladsen, såfremt FCK to timer senere vinder hjemmekampen mod
FC Midtjylland.</p><p x1="294" y1="602" x2="795" y2="1105">AaB var et af de
svageste hold i superligaen i foråret.</p
</block>]]></text>
<text><![CDATA[ <p x1="294" y1="602" x2="795" y2="1105">HORSENS - I
morgen kan være AaB's sidste dag som tophold i superligaen i denne
sæson.</p>
<p x1="294" y1="602" x2="795" y2="1105">AC Horsens kan sende AaB ned
fra førstepladsen, såfremt FCK to timer senere vinder hjemmekampen mod FC
Midtjylland.</p>
<p x1="294" y1="602" x2="795" y2="1105">AaB var et af de svageste hold
i superligaen i foråret.</p>
</p>
]]></text>
How do I avoid the encoding and enclose the text in CDATA-tags?
Martin Honnen - 25 Sep 2006 14:28 GMT
> nl.Item(0).InnerText = "<![CDATA[ " & textOutput & " ]]>"
If you want a CDATA section then you need to do e.g.
nl.Item(0).InnerXml = "<![CDATA[ " & textOutput & " ]]>"
That is one way possible and works independent of the existing or non
existing content of that item.
If you know that the item already has exactly one child which is a CDATA
section then you can simply do
nl.Item(0).FirstChild.Value = textOutput
As for doing
x.GetElementsByTagName("text")
you might want to use
x.SelectNodes("//text")
instead due to shortcomings of .NET's implementation of GetElementsByTagName
<http://support.microsoft.com/kb/823928/en-us>

Signature
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/