Currently, I have the following c# code to extract a value out of text. If its XML, I want the value within it – otherwise, if its not XML, it can just return the text itself.
String data = "..."
try
{
return XElement.Parse(data).Value;
}
catch (System.Xml.XmlException)
{
return data;
}
I know exceptions are expensive in C#, so I was wondering if there was a better way to determine if the text I’m dealing with is xml or not?
I thought of regex testing, but I dont’ see that as a cheaper alternative. Note, I’m asking for a less expensive method of doing this.
You could do a preliminary check for a < since all XML has to start with one and the bulk of all non-XML will not start with one.
(Free-hand written.)
I originally had the use of a regex but Trim()[0] is identical to what that regex would do.