BikerP
BikerP

Reputation: 868

Prevent XDocument or XElement form Encoding content

When I call XDocument.Save it is encoding my html <br/> tag, is there a way to prevent this?

XDocument xDoc = new XDocument(new XElement("desc","jon skeet <br/> knows, the <br/> answer"));
xDoc.Save(Server.MapPath("~/tempUploads/encodeTest.xml"));

OUTPUT IS:

<?xml version="1.0" encoding="utf-8"?>
<desc>jon skeet &lt;br/&gt; knows, the &lt;br/&gt; answer</desc>

OUTPUT I WOULD LIKE:

<?xml version="1.0" encoding="utf-8"?>
<desc>jon skeet <br/> knows, the <br/> answer</desc>

Upvotes: 2

Views: 4102

Answers (1)

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174309

That's expected behavior: You set the inner text of the XElement to that string. It needs to be encoded, otherwise it would create multiple tags.

As you actually want to have multiple tags, you need to create them. The easiest way would be to use XElement.Parse:

var content = XElement.Parse("<desc>jon skeet <br/> knows, the <br/> answer</desc>");
var xDoc = new XDocument(content);

Upvotes: 5

Related Questions