开发者

XMLEncode function in C#

开发者 https://www.devze.com 2023-04-12 02:13 出处:网络
I have got below XMLEncode function in VBScript function. I want to write similar function in C# 2.0 Function XMLEncode(byVal stringtoencode)

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="&lt;node&gt;it's my &quot;node&quot; &amp; i like it&lt;node&gt;">
    &lt;node&gt;it's my "node" &amp; i like it&lt;node&gt;
</xmlEncodeTest>
*/

From This site


Look for the HttpServerUtility.HtmlEncode-Method, maybe this can solve your problem.

0

精彩评论

暂无评论...
验证码 换一张
取 消