Append CDATA to a string

久未见 提交于 2019-12-10 17:27:32

问题


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#.


回答1:


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 + "]]>";
}


来源:https://stackoverflow.com/questions/5758099/append-cdata-to-a-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!