PageRenderTime 47ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/silverlight/test.xunit-silverlight/Compatibility/Silverlight3/MissingMethods/System.Xml.XPath.Extensions.cs

#
C# | 38 lines | 28 code | 4 blank | 6 comment | 0 complexity | b2860c908b413c288850fd2e32a8ad5f MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. using System.Collections.Generic;
  2. using System.Xml.Linq;
  3. using System.Linq;
  4. namespace System.Xml.XPath
  5. {
  6. internal static class Extensions
  7. {
  8. // VERY simple xpath helper method. Only useful for VERY simple expressions. Like what we need.
  9. // Thanks to Chris Cavanagh: http://chriscavanagh.wordpress.com/2009/04/11/micro-xpath-almost-in-silverlight-2/
  10. public static XElement XPathSelectElement(this XElement element, string expression)
  11. {
  12. return expression.Split('/').Aggregate(element, (e, name) => e.Element(name));
  13. }
  14. public static IEnumerable<XElement> XPathSelectElements(this XElement element, string expression)
  15. {
  16. // Split expression in parts
  17. // Get candidate elements for first part
  18. // Interrogate each candidate for 2nd part => new candidates
  19. // Interrogate each candidate for 3rd part
  20. var candidates = new List<XElement> {element};
  21. var parts = expression.Split('/');
  22. foreach (var part in parts)
  23. {
  24. var newCandidates = new List<XElement>();
  25. foreach (var candidate in candidates)
  26. {
  27. newCandidates.AddRange(candidate.Elements(part));
  28. }
  29. candidates = newCandidates;
  30. }
  31. return candidates;
  32. }
  33. }
  34. }