Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
here is my code,am trying to replace a particular sting to empty but its not happening,am not sure what am missing here.
C#
try
{
   transformedMsg = messageTransformer.transform@"C:\test.xsl",msgBody);
   //transformedMsg  returns value <?xml version="1.0" encoding="utf-16"?>
   string strxml = "<?xml version=" + "1.0" + " encoding=" + "utf-8" + "?>";
   string validtransmsg = transformedMsg.Replace(strxml, "");
   if(validtransmsg.Trim().Length>1)
   {
        DbMessageLoader.loadTransformedMessage(originalMsgId, msgTypeId, transformedMsg, sender);
   }
}
catch (Exception ex)
{
   logMessage("Transformation failed in External service: " + ex.Message + originalMsgId );
}




transformedMsg returns value ,am trying to replace this value to empty space then i will find the length.
but its not replace the value to empty,please advise me what am missing here
Posted
Updated 28-Jun-11 16:56pm
v2
Comments
J a a n s 29-Jun-11 1:26am    
Use string strxml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
Also check the encoding (utf-8 or utf-16).
shan1395 30-Jun-11 2:13am    
hi Jaans,

thanks that's suppose to post in solution,even i figured out am missing in the string.
Thanks

Remember, the instance of System.String is an immutable object. Please see my explanation and some implications in my past answer:
Difference Between Mutable , Immutable string in C#[^].

—SA
 
Share this answer
 
v2
String.Replace doesnt replace the original variable's value. Instead the output is saved to the new variable.

if u got no choice and still need to change it, then go for the below.
transformedMsg = transformedMsg.Replace(strxml, "");


http://msdn.microsoft.com/en-us/library/fk49wtc1.aspx[^]
public class ReplaceTest {
    public static void Main() {

        string errString = "This docment uses 3 other docments to docment the docmentation";

        Console.WriteLine("The original string is:{0}'{1}'{0}", Environment.NewLine, errString);

        // Correct the spelling of "document".

        string correctString = errString.Replace("docment", "document");

        Console.WriteLine("After correcting the string, the result is:{0}'{1}'",
                Environment.NewLine, correctString);
    }
}
//
// This code example produces the following output:
//
// The original string is:
// 'This docment uses 3 other docments to docment the docmentation'
//
// After correcting the string, the result is:
// 'This document uses 3 other documents to document the documentation'
//



Cheers
Balaji
 
Share this answer
 
J a a n s - 4 days ago
Use string strxml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"; Also check the encoding (utf-8 or utf-16).


Thanks to Jaans.
 
Share this answer
 
Comments
Christian Graus 3-Jul-11 22:03pm    
Please don't push 'answer' to add a comment. Use the 'comment' button, as I did.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900