Here’s a small extension method to roughly determine if a string contains an XML fragment (it’s not foolproof, but is usually adequate):
private static Regex isXmlFragment = new Regex( @"<([^>]+)>(.*?</(\1)>|[^>]*/>)" ); public static bool IsXmlFragment( this string content ) { return isXmlFragment.IsMatch( content ); }
Of course you could just use the regular expression by itself if you prefer…


1 comment
Comments feed for this article
April 16, 2009 at 7:06 am
Dew Drop - April 16, 2009 | Alvin Ashcraft's Morning Dew
[...] “Is XML” Regex / Extension Method (Chris Cavanagh) [...]