C# method to check if character is markup or not
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 man开发者_如何学Pythonually typing the characters.
You may checkout the following KB article.
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.)
You can also use this code
const string XMLCHARS = "'\"\\<>&";
if(XMLCHARS.Contains(c))
{
--
}
You can use extension
public static class CharExtension
{
public static bool IsXmlMarkup(this char charecter)
{
if(charecter == '\'' || charecter == '\"' || e.t.c)
return true;
return false;
}
}
and then just use
char c = '\'';
var res = c.IsXmlMarkup();
精彩评论