I have the following query:
Dim elementsWithPossibleCCNumbers As IEnumerable(Of XElement) =
xmlTree.Descendants().
Where(Function(element) element.Attributes().
Where(Function(attribute) attribute.Value.Length >= 13
AndAlso attribute.Value.Length <=
16).
Where(Function(attribute) Long.TryParse(attribute.Value, numeric)).Count() = 1).
[Select](Function(x) x)
I originally was searching for attributes using Regex, but I started using LINQ to XML due to some others in the community telling me it is the better route to go. My only concern is that for larger XML strings, how is the performance with the LINQ to XML queries and is it fast than Regex anyway?
Can I improve the speed of the query?
The primary concern is that regexp is fundamentally the wrong tool for processing XML files. It is far too easy to trick regexp into returning wrong results to you, for example by adding a
CDATAsection. The speed is by far the secondary concern: it is impossible to even tell if XML is well-formed with a regex, let alone processing corner cases correctly.Now to answer your question, the speed depends a lot on the structure of your regular expression. It is possible to construct a file/regexp combination where regexp would be faster, and another pair of file/regexp where the LINQ2XML route would be faster. But LINQ2XML would still be reasonably fast, and it will most likely be more correct.