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

We have detected that you are using extensions to block ads. We need money to operate the site, and almost all of it comes from online advertising. Please support us by disabling these ads blocker.

Please disable ad blocker