invalid char xml

I am getting an xml which has a lot of invalid charachters like ° in c#.net.Is it possible to encode this character in code so it doesnt error out
LVL 4
Moizsaif123Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Rikin ShahConnect With a Mentor Microsoft Dynamics CRM ConsultantCommented:
Hi,

You can use XmlConvert.EncodeName method to convert invalid chars to valid xml chars-


void Main() {
    string content = "\v\f\0";
    Console.WriteLine(IsValidXmlString(content)); // False

    content = XmlConvert.EncodeName(content);
    Console.WriteLine(IsValidXmlString(content)); // True
}

static bool IsValidXmlString(string text) {
    try {
        XmlConvert.VerifyXmlChars(text);
        return true;
    } catch {
        return false;
    }
}

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.