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.
If all you need to do is view the data in an XML file, you're in luck. Just about every browser can open an XML file. In Chrome, just open a new tab and drag the XML file over. Alternatively, right click on the XML file and hover over "Open with" then click "Chrome".
Very simple: Valid JSON starts always with '{' or '[' Valid XML starts always with '<'
You could try to parse the string into an XDocument. If it fails to parse, then you know that it is not valid. string xml = ""; XDocument document = XDocument. Parse(xml);
As a result, all tag names and content in an XML document are indexed and searchable using the Keyword field. A keyword search for either “catalog” or “title” returns the XML document. To limit your keyword search to XML documents, you can enter xml: followed by a specialized query string.
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.)
// Has to have length to be XML if (!string.IsNullOrEmpty(data)) { // If it starts with a < after trimming then it probably is XML // Need to do an empty check again in case the string is all white space. var trimmedData = data.TrimStart(); if (string.IsNullOrEmpty(trimmedData)) { return data; } if (trimmedData[0] == '<') { try { return XElement.Parse(data).Value; } catch (System.Xml.XmlException) { return data; } } } else { return data; }
I originally had the use of a regex but Trim()[0] is identical to what that regex would do.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With