Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Better way to detect XML?

Tags:

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.

like image 528
cyberconte Avatar asked May 21 '09 03:05

cyberconte


People also ask

How to read data from XML?

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".

How do I know if I have XML or JSON?

Very simple: Valid JSON starts always with '{' or '[' Valid XML starts always with '<'

How check string is XML or not in C#?

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);

How do I search in 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.


1 Answers

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.

like image 109
Colin Burnett Avatar answered Oct 05 '22 23:10

Colin Burnett