CDATA追加为一个字符串 [英] Append CDATA to a string
问题描述
我的情况是,我们使用合同第一种方法为Web服务。我必须使用CDATA来避免特殊字符,需要被附加到当前字符串变量。什么是一个CDATA标签附加到当前字符串,这是在响应对象返回XML元素的最佳方法?我们正在使用C#。
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#.
推荐答案
您可以使用的 XCData 从Linq到XML库,它应该全自动环绕字符串中的CDATA标记构建。
You can use the XCData construct from the Linq-to-XML Library, which should automaticly wrap a CData tag around a string.
示例代码:
//Assuming your string is called @string
XCData cdata = new XCData(@string);
//CData string
string cdataString = cdata.ToString();
如果您没有访问XLINQ构造,你可以只是做以下
If you do not have access to XLinq constructs you could just do the following
private string WrapInCData(string @string)
{
return "<![CData[" + @string + "]]>";
}
这篇关于CDATA追加为一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!