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

Language C# Lines 38
MD5 Hash b2860c908b413c288850fd2e32a8ad5f Estimated Cost $633 (why?)
Repository https://hg01.codeplex.com/xunitcontrib View Raw File
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
using System.Collections.Generic;
using System.Xml.Linq;
using System.Linq;

namespace System.Xml.XPath
{
    internal static class Extensions
    {
        // VERY simple xpath helper method. Only useful for VERY simple expressions. Like what we need.
        // Thanks to Chris Cavanagh: http://chriscavanagh.wordpress.com/2009/04/11/micro-xpath-almost-in-silverlight-2/
        public static XElement XPathSelectElement(this XElement element, string expression)
        {
            return expression.Split('/').Aggregate(element, (e, name) => e.Element(name));
        }

        public static IEnumerable<XElement> XPathSelectElements(this XElement element, string expression)
        {
            // Split expression in parts
            // Get candidate elements for first part
            // Interrogate each candidate for 2nd part => new candidates
            // Interrogate each candidate for 3rd part

            var candidates = new List<XElement> {element};
            var parts = expression.Split('/');
            foreach (var part in parts)
            {
                var newCandidates = new List<XElement>();
                foreach (var candidate in candidates)
                {
                    newCandidates.AddRange(candidate.Elements(part));
                }
                candidates = newCandidates;
            }

            return candidates;
        }
    }
}
Back to Top