Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / XML / September 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Klaus Jensen - 25 Sep 2006 13:43 GMT
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 - 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/


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.