c# - Select XML element without using Extension method -


Says:

There is some performance penalty for using these methods I LINQ Using XML queries gets better performance

and there is an extension method

I have to follow XML, I need to find and add messages. The challenge is that I only have to select the messages under the Status / Status Message / Status Date . With descendants , I am receiving all the messages - even outside of the essential elements.

This can be achieved correctly by using XPathSelectElement . But since XPathSelectElement is an extension method, it has some performance hits that have been shown as shown in:

In most cases running the XPath query, using standard LINQ, compared to queries 5 There will be more execution period times

Note: What to tweak to Is there any way to do this? >

XML

  XDocument xDoc = XDocument.Parse (@ "  for this purpose  & Gt; & lt; StatusType & gt; Invoice & lt; / StatusType & gt; & lt; StatusDetail & gt; & lt; Sequence Trial = "" & gt; 2 & lt; / Sequence & gt; & Lt; Message & gt; & lt; / Message & gt; & lt; / StatusDetail & gt; & lt; StatusDetail & gt; & lt; Message & gt; B & lt; / Message & gt; & lt; / StatusDetail & Gt; & lt; StatusDetail & gt; & lt; Message & gt; C & lt; / Message & gt; & lt; / StatusDetail & gt; & lt; / StatusMsg & gt; & lt; StatusDetail & gt; & Lt; Message & gt; Out & lt; / Message & gt; & lt; / StatusDetail & gt; & lt; / Status & gt; "); MessageArrayWithOutside = xDoc.Descendants (@ "StatusDetail") on   

Code

  // .Select (X = & X. Element ("Message") == False? String.Epti: X. Element ("Message"). Value.Trim ()) .Oir (); Var textAll = string.Join (",", messaging with text); // XPathSelectElements on messageArray = xDoc.XPathSelectElements (@ "Status / StatusMsg / StatusDetail") .Select (x = & gt; x.Element ("message") == Null String.Empty: x.Element ("Message") Value.Rim ()) .Oir (); Var text = string.Join (",", messages); Exagath looks faster than using twice.   

UPDATE

Any ideas why?

  // Mercury stopwatch stopwatchdesands = new stopwatch (); StopWatchDescendants.Start (); MessageArrayDecendants = xDoc.Descendants ("StatusMsg") .Descendants ("StatusDetail"). Selection (x => x.Element ("message") == null string.Empty: x.Element ("message"). Value.Trim ()) .oir (); Var textDecendants = string.Join (",", Message ArrayCendants); StopWatchDescendants.Stop (); Timespace tsDescendants = stopWatchDescendants.Elapsed; // XPathSelectElements stopwatch stop WatchXPath = new stopwatch (); StopWatchXPath.Start (); MessageArrayXPath = xDoc.XPathSelectElements (@ "Status / StatusMsg / StatusDetail") .Select (x => x.Element ("message") == Null String.Empty: x.Element ("message") value. Trim ()) .oir (); Var textXpath = string.Join (",", messageArrayXPath); StopWatchXpath.Stop (); Timespain TSXpath = Stopwatchxpath Finished; If (tsXPath> tsDescendants) {Console.WriteLine ("LINQ is fast"); } If (tsDescendants> tsXPath) {Console.WriteLine ("XPath is fast"); } Console.reiteline ("exposed:" + tsxpath.ToString ()); Console.light line ("LINQ:" + tsDescendants.ToString ());    

You need to use the following:

  var messageArrayWithOutside = xDoc.Elements ("StatusMsg"). Elements ("StatusDetail"). Select (x = & gt; x.Element ("message") == empty? String.Empty: x.Element ("Message") .Value.Trim ()) .oir (); Var textAll = string.Join (",", messaging with text);   

Text will contain the desired output in all strings and OUTSIDE :

  A, B, C   

The key is being used, which limits the search that xDocument is intended for immediate children.

Comments

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

github - Teamcity & Git - PR merge builds - anyway to get HEAD commit hash? -

ios - Replace text in UITextView run slowly -