Replace special character from a string and pass to xml

Rock Hitman 46 Reputation points
2020-11-13T03:34:06.53+00:00

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;

}

Community Center | Not monitored
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Tianyu Sun-MSFT 34,441 Reputation points Microsoft External Staff
    2020-11-13T08:21:11.79+00:00

    Hi @Rock Hitman ,

    Thank you for taking time to post this issue in Microsoft Q&A forum.

    Which type of project and programming language do you use? Do you use C#?

    If you use C#, as you know, C# is currently not supported in the Microsoft Q&A forums, the supported products are listed over here: https://learn.microsoft.com/en-us/answers/products/ (more to be added later on).

    For the related questions about C#, you can post here: MSDN Visual C#.

    Thank you for your understanding.

    Sincerely,
    Tianyu

    • If the answer is helpful, please click "Accept Answer" and upvote it.
      Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
    0 comments No comments

  2. David Lowndes 4,726 Reputation points
    2020-11-13T09:23:33.547+00:00

    If you don't really want to have to remove characters, you need to escape the string before it can be safely saved as XML.
    Have a look here to see which proposed solution meets your need.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.