Alex Rivera | Logout

Replace & with & in C#

Asked 2010-09-30T19:42:09.463
12

Ok I feel really stupid asking this. I see plenty of other questions that resemble my question, but none seem to be able to answer it.

I am creating an xml file for a program that is very picky about syntax. Sadly I am making the XML file from scratch. Meaning, I am placing each line in individually (lots of file.WriteLine(String)).

I know this is ugly, but its the only way I can get the logic to work out.

ANYWAY. I have a few strings that are coming through with '&' in them.

if (value.Contains("&"))
   {
      value.Replace("&", "&");
   }

Does not seem to work. The value.Contains() seems to see it, but the replace does not work. I am using C# .Net 2.0 sp2. VS 2005.

Please help me out here.. Its been a long week..

Edit
Report

1 Answer

3

you can also use HttpUtility.HtmlEncode class under System.Web namespace instead of doing the replacement yourself. here you go: http://msdn.microsoft.com/en-us/library/73z22y6h.aspx

answered 2010-09-30T19:53:09.557

Your Answer