How to manage special characters in XML element using C#

BeUnique 2,332 Reputation points
2023-06-20T08:36:02.8966667+00:00

This is very simple question and something reasonable.

I am handling XML file through C# code. each XML file contains millions of records.

Inside the XML tag, some value contains special characters like "–". But, this doesn't allow to process in XML for inserting to SQL.

It's showing error like "illegal xml character"

How to handle such type of special characters....?

Example XML Tag below

<Employee EmpId="528496">
			<NAME>JAMES–SS</NAME>
</Employee>

"DRUMS–SS" ==> "–"(Special character) and doesn't allow to process in SQL.

"DRUMS–SS" ==> "–"(Special character) and doesn't allow to process in SQL.

How to handle such type of special characters....?

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,486 questions
{count} votes

1 answer

Sort by: Most helpful
  1. BeUnique 2,332 Reputation points
    2023-06-21T10:16:49.1333333+00:00

    Hi @Anonymous ,

    Thanks for your reply.

    I used below code for handling illegal characters.

     public static string JustAscii(string source)
            {
                return new string(source.Where(c => c <= sbyte.MaxValue).ToArray());
            }
    

    It's working now....Thanks for your support and help..

    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.