Forum Moderators: open

Message Too Old, No Replies

Parsing string containing XML

         

johnblack

3:50 am on Sep 1, 2010 (gmt 0)



Hi,

I'm sure there's something embedded in the .NET framework to do this, but I just can't seem to find it.

I've got a string variable that contains xml text

e.g. <tag>data</tag>

I load this string into an XmlDocument using

xmlDoc.Load(New StringReader("<tag>data</tag>"))

Which all works fine when the text "data" doesn't contain '&', '<', '>' etc.

What I'm after is a .NET function that will either correctly convert the data items in my string prior to loading into the XmlDocument object or a function that handles the string 'warts and all' and creates the XmlDocument correctly.

Currently I'm having to parse the text by hand to find the characters and replace them.

Hope that all makes sense.

marcel

5:05 am on Sep 1, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry, I haven't much time at the moment, hopefully this will get you in the right direction though.

In an XML document, data containing 'special' characters need to be wrapped in CDATA section.

ie.
 
<tag>
<![CDATA[
data containing <>&
]]>
</tag>

here is some more info:
[w3schools.com...]

johnblack

8:25 pm on Sep 1, 2010 (gmt 0)



Thanks for that Marcel,

That's a great solution, however we'd have to change the code that creates the XML string which we don't want to do right now.

So it looks as if I'll have to parse the string for the moment as a quick fix.

If there's anything else out there though that you are aware of ...

Cheers John