Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append CDATA to a string

Tags:

c#

xml

cdata

My situation is that we are using contract first method for web services. I have to use CDATA to avoid special characters, which needs to be appended to our current string variable. What would be the best method to append a CDATA tag to our current string, which is returned as xml element in response object? We are using C#.

like image 762
OBL Avatar asked Dec 22 '22 15:12

OBL


1 Answers

You can use the XCData construct from the Linq-to-XML Library, which should automaticly wrap a CData tag around a string.

Example code:

//Assuming your string is called @string
XCData cdata = new XCData(@string);
//CData string
string cdataString = cdata.ToString();

If you do not have access to XLinq constructs you could just do the following

private string WrapInCData(string @string)
{
   return "<![CData[" + @string + "]]>";
}
like image 95
Yet Another Geek Avatar answered Dec 29 '22 01:12

Yet Another Geek