0

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

2
  • Can you show what's in rawXml variable? Commented Jul 2, 2013 at 17:09
  • @lazyberezovsky that is <Description>&lt;![CDATA[some interesting stuff \r\n]]&gt;</Description> Commented Jul 3, 2013 at 18:07

1 Answer 1

1
XElement.Parse("<root>"+myValue+"</root>").Value
Sign up to request clarification or add additional context in comments.

2 Comments

Seems pretty clean. Why would you choose this over stripping out the start and end of the string, as I suggested?
It doesn't depend on the exact characters used, just that it is double-encoded XML.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.