Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# method to check if character is markup or not

Tags:

c#

.net

xml

Is there any method in the .NET framework which will return true if a given character is an XML markup character? That is, one of the characters, '"<>_&, and any others that may exist.

I understand I can go for a simple string search also, but was wondering if a built-in method exists which would not rely on manually typing the characters.

like image 478
Shamim Hafiz - MSFT Avatar asked Nov 28 '25 09:11

Shamim Hafiz - MSFT


2 Answers

You may checkout the following KB article.

like image 154
Darin Dimitrov Avatar answered Nov 29 '25 23:11

Darin Dimitrov


I'm not sure the term "XML markup character" (or rather a context-independent function for detecting these) makes much sense, since some of the characters you list only have special meaning depending on the context in which they appear (such as ' and ", which are normal characters if they appear outside of a tag).

Apart from that, you could always write your own such function:

bool IsMarkupCharacter(char ch)
{
    switch (ch)
    {
        case '\'':
        case '\"':
        case '<':
        case '>':
        case '&':
            return true;

        default:
            return false;
    }
}

Of course you would want to check this against the XML specification to check if it's truly complete. (I didn't include _ from your list, by the way; it is not special to XML in any way, AFAIK.)

like image 30
stakx - no longer contributing Avatar answered Nov 29 '25 22:11

stakx - no longer contributing



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!