I have got below XMLEncode function in VBScript function. I want to write similar function in C# 2.0
Function XMLEncode(byVal stringtoencode)
Dim strTemp ' As String
strTemp = stringtoencode
strTemp开发者_如何学C = Replace( strTemp, chr(38), "&" )
strTemp = Replace( strTemp, chr(34), """ )
strTemp = Replace( strTemp, chr(60), "<" )
strTemp = Replace( strTemp, chr(62), ">" )
strTemp = Replace( strTemp, chr(169), "©" )
XMLEncode = strTemp
End Function
Please suggest!! if there is any in built function in c# or do I need to write same logic in C#
string xml = "<node>it's my \"node\" & i like it<node>";
using (XmlTextWriter xtw = new XmlTextWriter(@"c:\xmlTest.xml", Encoding.Unicode))
{
xtw.WriteStartElement("xmlEncodeTest");
xtw.WriteAttributeString("testAttribute", xml);
xtw.WriteString(xml);
xtw.WriteEndElement();
}
// RESULT:
/*
<xmlEncodeTest testAttribute="<node>it's my "node" & i like it<node>">
<node>it's my "node" & i like it<node>
</xmlEncodeTest>
*/
From This site
Look for the HttpServerUtility.HtmlEncode-Method, maybe this can solve your problem.
精彩评论