Nikhil Agrawal
Nikhil Agrawal

Reputation: 48558

How to have special characters in XML File and read it correctly?

I am creating an XML File where a tag's Innertext is \r\n

It is creating fine. But when i read these xml files in code-behind \r\n gets read as \n\n

I am reading it through XMLTextReader.

What could be the reason for this and how to read it like it was provided?

Upvotes: 1

Views: 1482

Answers (2)

Oded
Oded

Reputation: 498904

You need to encode such characters in order for them to be preserved.

One approach is to replace the characters with the respective character reference:

  • \n to 

  • \r to 

Another one would be to enclose sections that need to preserve whitespace in <![CDATA[]]> sections.

Upvotes: 2

user504342
user504342

Reputation: 965

The question doesn't provide enough details to correctly answer it. So I will be assuming things here.

A few things should be considered:

  1. Are you making use of different Locale settings?
  2. What is the encoding of the XML file?
  3. What is the platform encoding of the system?
  4. What are the systems line separator(s)?
  5. Are you using the innertext to force a line break in a document that is built up from this XML file?
  6. Are you aware of the CDATA tag in the XML language?

These points can be of influence to your problem. But when re-reading your question, I think it is the CDATA solution you are looking for to keep your innertext from being misinterpreted by an XML parser.

My guess is to wrap the \r\n innertext in a CDATA tag.

More info on a CDATA tag can be found here: CDATA

Good luck!

Upvotes: 0

Related Questions