locked
Replace special character from a string and pass to xml RRS feed

  • Question

  • Hi, I am receiving a special character like '&' in my string which is being passed to xml and it is failing.

    sting a = "Tom & Jerry"

    How easily can i remove this & with & and pass to the xml ?

    Please advice


    if(a.Contains("'@' || '#' || '$' || '&' || '%' || '!' || '~'"))
    {
    a= a.Replace("&");

    return a;

    }


    RH

    Friday, November 13, 2020 3:46 AM

Answers


  • sting a = "Tom & Jerry"

    if(a.Contains("'@' || '#' || '$' || '&' || '%' || '!' || '~'"))
    {
    a= a.Replace("&");

    return a;

    }



    Your syntax for both the if() and the Contains() is all wrong.

    Using the logical OR operator (||) in the Contains method is wrong. That will not
    be testing for what you want. You would need a compound conditional such as:

    if (a.Contains('@') 
        || a.Contains('#')
        || a.Contains('$') 
        || a.Contains('&') 
        // ... etc.
        
        )
        {
        a = a.Replace("&", "&amp"); // note 2 arguments
        // ... etc.
        
        }
    

    The if() isn't really needed, as you can just do a Replace and it will
    change any that are there or it will simply do nothing if there aren't any.
        
    - Wayne

    • Marked as answer by Dan2890 Friday, November 13, 2020 4:08 PM
    Friday, November 13, 2020 5:07 AM

All replies


  • sting a = "Tom & Jerry"

    if(a.Contains("'@' || '#' || '$' || '&' || '%' || '!' || '~'"))
    {
    a= a.Replace("&");

    return a;

    }



    Your syntax for both the if() and the Contains() is all wrong.

    Using the logical OR operator (||) in the Contains method is wrong. That will not
    be testing for what you want. You would need a compound conditional such as:

    if (a.Contains('@') 
        || a.Contains('#')
        || a.Contains('$') 
        || a.Contains('&') 
        // ... etc.
        
        )
        {
        a = a.Replace("&", "&amp"); // note 2 arguments
        // ... etc.
        
        }
    

    The if() isn't really needed, as you can just do a Replace and it will
    change any that are there or it will simply do nothing if there aren't any.
        
    - Wayne

    • Marked as answer by Dan2890 Friday, November 13, 2020 4:08 PM
    Friday, November 13, 2020 5:07 AM
  • When you say "being passed to xml", what exactly do you mean?  If you pass a string containing a special character to an XML serializer, it will automatically convert the special characters to entities.  You don't have to do that.

    If you are creating XML by hand, then you need to read about the entities.  The only ones you have to worry about are < > " ' and &.


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Friday, November 13, 2020 6:07 AM
  • You need to run input text you receive through an HTML encoder. Do not attempt to do this yourself because you're going to do it wrong. For example you are not even taking the element delimiters into account. Hand coding this is never the right solution.

    To HTML encode a string use the WebUtility.HtmlEncode method. Pass it the input text and it returns the text with any HTML appropriate characters encoded.


    Michael Taylor http://www.michaeltaylorp3.net

    Friday, November 13, 2020 2:36 PM