Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace & with & in C#

Tags:

string

c#

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..

like image 626
Ian Kremer Avatar asked Sep 30 '10 19:09

Ian Kremer


People also ask

What do u mean by replace?

replace, displace, supplant, supersede mean to put out of a usual or proper place or into the place of another. replace implies a filling of a place once occupied by something lost, destroyed, or no longer usable or adequate. replaced the broken window displace implies an ousting or dislodging.

What is another synonym for replace?

Some common synonyms of replace are displace, supersede, and supplant.

Have been replaced meaning?

to take the place of something, or to put something or someone in the place of something or someone else: The factory replaced most of its workers with robots.

What does it mean to replace something with something else?

To replace is to substitute one thing for another — in this case, to get a new pen and throw the old one away. When you see the word place in it, replace makes sense: replacing is putting something new in place of something old.


2 Answers

If you really want to go that route, you have to assign the result of Replace (the method returns a new string because strings are immutable) back to the variable:

value = value.Replace("&", "&");

I would suggest rethinking the way you're writing your XML though. If you switch to using the XmlTextWriter, it will handle all of the encoding for you (not only the ampersand, but all of the other characters that need encoded as well):

using(var writer = new XmlTextWriter(@"C:\MyXmlFile.xml", null))
{
    writer.WriteStartElement("someString");
    writer.WriteText("This is < a > string & everything will get encoded");
    writer.WriteEndElement();
}

Should produce:

<someString>This is &lt; a &gt; string &amp; 
    everything will get encoded</someString>
like image 73
Justin Niessner Avatar answered Nov 15 '22 21:11

Justin Niessner


You should really use something like Linq to XML (XDocument etc.) to solve it. I'm 100% sure you can do it without all your WriteLine´s ;) Show us your logic?

Otherwise you could use this which will be bullet proof (as opposed to .Replace("&")):

var value = "hej&hej<some>";
value = new System.Xml.Linq.XText(value).ToString(); //hej&amp;hej&lt;some&gt;

This will also take care of < which you also HAVE TO escape :)

Update: I have looked at the code for XText.ToString() and internally it creates a XmlWriter + StringWriter and uses XNode.WriteTo. This may be overkill for a given application so if many strings should be converted, XText.WriteTo would be better. An alternative which should be fast and reliant is System.Web.HttpUtility.HtmlEncode.

Update 2: I found this System.Security.SecurityElement.Escape(xml) which may be the fastest and ensures max compatibility (supported since .Net 1.0 and does not require the System.Web reference).

like image 30
Lasse Espeholt Avatar answered Nov 15 '22 20:11

Lasse Espeholt