dampee
dampee

Reputation: 3447

Decode Xdata stored in string in c#

I have a string with CData encoded value in it. How can I decode this properly. Using XElement.Parse() throws an error because there is no root element.

var rawXml = GetFromExternalService();
var myValue = XElement.Parse(rawXml).Element("Description").Value;

The variable myValue is now <![CDATA[some interesting stuff \r\n]]>. So in fact this is an error from the service, double encoding the value. But I can't change that.

I could just strip the first 9 and the last 3 characters, but that just feels so wrong.

What is the best way to get the real value?

Kind regards
Damiaan

Upvotes: 0

Views: 257

Answers (1)

Markus Jarderot
Markus Jarderot

Reputation: 89221

XElement.Parse("<root>"+myValue+"</root>").Value

Upvotes: 1

Related Questions