views:

179

answers:

5

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

+19  A: 

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>
Justin Niessner
lasseespeholt
OH my god.. Now I feel very stupid.. I should take a nap. Thank you
Ian Kremer
+1  A: 

You can try:

value = value.Replace("&", "&amp;");
Pablo Santa Cruz
A: 

Strings are immutable. You need to write:

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

Note that if you do this and your string contains "&amp;", it's going to get changed to "&amp;amp;".

Jim Mischel
Yes I will make sure to test for that in the if Statement. Thanks
Ian Kremer
Jeffrey L Whitledge
Jim Mischel
+4  A: 

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

lasseespeholt
+1 nice to know. T
kenny
I am sure you are right. The structure of the resulting XML document changes quite a bit depending on what types of values I pull out of another xml document. Somewhat hard to explain, but I will take note of what you suggested so once I get past this "GET IT DONE NOW" phase I can actually make it non-ugly. Thanks
Ian Kremer
Although you missed the actual problem with the code, I will give you a +1 for suggesting XText.
Yogesh
Just a note. I am stuck using .Net 2.0 sp2. Am I right to assume Linq is a >=3.0 feature?
Ian Kremer
@Ian It does not exists in .Net 2.0. You may want to use HtmlEncode instead :) This site show a few options: http://weblogs.sqlteam.com/mladenp/archive/2008/10/21/Different-ways-how-to-escape-an-XML-string-in-C.aspx
lasseespeholt
@lasseespeholt Ya that may be my best option. Much better than a giant multi Replace statement. Thanks
Ian Kremer
+3  A: 

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

davsan
Be ware though that this class is not in the ClientProfile
Henk Holterman