r-magalhaes
r-magalhaes

Reputation: 458

Convert special character (<,>,&) of C# string to XML using xElement

C#

noticia.Add(new XElement("Imagem", <BR>));

i need :

<Imagem><BR></Imagem>

and not:

<Imagem>&lt;BR&gt;</Imagem>

thanks all

Upvotes: 1

Views: 2085

Answers (2)

r-magalhaes
r-magalhaes

Reputation: 458

I realy use classes:

I recive from on class the string:

string OutClass = "xpto. <BR>";

and i use the XElement() in another class.

Upvotes: 0

CaffGeek
CaffGeek

Reputation: 22064

Just do this

noticia.Add(new XElement("Imagem", new XElement("BR")));

However that will give you an extra / which you NEED or it's not valid XML.

<Imagem><BR/></Imagem>

The other options is using CDATA

noticia.Add(new XElement("Imagem", new XCData("<BR>")));

Which will get you

<Imagem><![CDATA[<BR>]]></Imagem>

Just generating <Imagem><BR></Imagem> is impossible as it's not valid xml.

EDIT: If you have the string with other text, in a variable, your only option is CDATA, like this

var OutClass = "xpto. <BR>";
noticia.Add(new XElement("Imagem", new XCData(OutClass)));

Which results in

<Imagem><![CDATA[xpto. <BR>]]></Imagem>

Upvotes: 5

Related Questions