Martin Delille
Martin Delille

Reputation: 11780

Carriage return in XElement

I want to produce an XML file where carriage return in elements are represented by the following sequence 
 but I'm unable get:

<elem>Hello&#13;world!</elem>

If I do the following:

var elem = new XElement("elem", "Hello\nworld!");

It produces:

<elem>Hello
world!</elem>

If I do the following:

var elem = new XElement("elem", "Hello&#13;world!");

It produces:

<elem>Hello&amp;#13;world!</elem>

Any ideas?

Upvotes: 0

Views: 786

Answers (1)

Justin Russo
Justin Russo

Reputation: 2214

Okay your answer is pretty clear. Anytime you need to write special chars into xml values, you would use CDATA.

Here's how you do it.

<elem><![CDATA[Hello&#13;world!]]></elem>

This is the fix you are looking for.

[UPDATE] The only reason I stumbled on this answer initially, was because I was thinking about HTML. However, if you were writing this in HTML, you would simply use a <br /> tag.

Upvotes: 1

Related Questions