Hi,
I have a xml value in string which is having almost 500 characters.In that string if there is a character '<' I need to replace it with '>' in C#.Thanks in advance
Thanks,
Prasant
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Jignesh TrivediPosted Jun 25, 2012, 11:44 PM
If you develped web application then you can also use "HttpUtility.HtmlEncode" which is from "System.Web" name space.
example:
string xml = @"
Don't forget
;
string encodedXml = System.Web.HttpUtility.HtmlEncode(xml);
Output :
<?xml version='1.0' encoding='ISO-8859-1'?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<heading>1 < 2</heading>
<!-- This is a comment -->
<body>Don't forget <me this weekend!<br /></body>
</note>
please refer
http://weblogs.sqlteam.com/mladenp/archive/2008/10/21/Different-ways-how-to-escape-an-XML-string-in-C.aspx
hope this will help you.
VulpesPosted Jun 25, 2012, 7:30 PM
The idea is to first isolate all the tags and replace the '<' with some special character that won't otherwise appear in the XML string. I've used '~' for now but you can use a different character if you wish.
After doing this you can then find the remaining '<' which appear in the text and replace them with the corresponding entity <
Finally, you can replace the '~' with '<' to restore the tags.
The output should be: