Groups | Blog | Home
all groups > dotnet xml > september 2006 >

dotnet xml : Opening and altering XML-doc using XmlDocument wrecks havoc on doc


Klaus Jensen
9/25/2006 2:43:42 PM
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?

Martin Honnen
9/25/2006 3:28:34 PM


[quoted text, click to view]


[quoted text, click to view]

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
AddThis Social Bookmark Button