469,950 Members | 1,802 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,950 developers. It's quick & easy.

Opening and altering XML-doc using XmlDocument wrecks havoc on doc

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>&lt;![CDATA[ &lt;p x1="294" y1="602" x2="795" y2="1105"&gt;HORSENS - I
morgen kan være AaB's sidste dag som tophold i superligaen i denne
sæson.&lt;/p&gt;
&lt;p x1="294" y1="602" x2="795" y2="1105"&gt;AC Horsens kan sende AaB ned
fra førstepladsen, såfremt FCK to timer senere vinder hjemmekampen mod FC
Midtjylland.&lt;/p&gt;
&lt;p x1="294" y1="602" x2="795" y2="1105"&gt;AaB var et af de svageste hold
i superligaen i foråret.&lt;/p&gt;
&lt;/p&gt;
]]&gt;</text>

How do I avoid the encoding and enclose the text in CDATA-tags?
Sep 25 '06 #1
1 1192


Klaus Jensen wrote:

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>
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Sep 25 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Derrick | last post: by
6 posts views Thread by Kenneth P | last post: by
9 posts views Thread by JimmyKoolPantz | last post: by
2 posts views Thread by doc1355 | last post: by
dbrewerton
19 posts views Thread by dbrewerton | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.