Xelement converting special characters to strings

The below XElement converts the special character "&" to "&".

XElement newElement = new XElement("testting", "wow&testvalue");

I want it to be the "&" not &.

Jon Skeet
people
quotationmark

I want it to be the "&" not &.

Then it would be invalid XML. Why do you want invalid XML?

LINQ to XML is expressing the text you've requested in valid XML. That's what it's meant to do. If you ask for the text of the element later (through this or any other decent XML API) you'll get back wow&testvalue.

As Tim says, you could use a CDATA section:

var element = new XElement("testing", new XCData("wow&testvalue"));

But you can't tell LINQ to XML not to escape what it needs to escape...

people

See more on this question at Stackoverflow