Skip to Content

Solved: How to fix the error when loading XML from text box with special characters

Problem Symptom

I have below code to generate/formatting XML from the content of a text box:

Dim xmlDoc As New XmlDocument
xmlDoc.LoadXml(Textbox)
Dim writer As New XmlTextWriter("C:\Work\xmlWriterTest.xml", Nothing)
writer.Formatting = Formatting.Indented
xmlDoc.Save(writer)

It works well, but when I have for example a special character like (&) character I get an error in LoadXml.

How can I fix this error or change encoding?

Follow the below solution steps to resolve the error when loading XML from text box with special characters.

There are few ways to handle it. One of the two methods below

Solution 1: Encode & Decode the XML data

'store it
xmlDoc.LoadXml(HttpServerUtility.HtmlEncode(Textbox))

'read it
Dim xmlDoc as New XmlDocument()
xmlDoc= XDocument.Parse(HttpUtility.HtmlDecode(ResponseData))

Solution 2: Parse XML data wrap with CDAT

<![CDATA[
   characters with markup
]]>

Reference

Tags

Tags

    Ads Blocker Image Powered by Code Help Pro

    Ads Blocker Detected!!!

    This site depends on revenue from ad impressions to survive. If you find this site valuable, please consider disabling your ad blocker.