예제 #1
0
        public static XElement Parse(XElement source, Func <XElement, bool> parse)
        {
            var clone  = CloneRoot(source);
            var parser = new XEnumeratorElement(source);

            return(parser.ChildrenInto(clone, parse));
        }
예제 #2
0
        IEnumerable <XNode> DescendantsParse(XContainer node, Func <XElement, bool> parse)
        {
            Reset(node);
            XNode root = nodes.Any() ? nodes.First().Parent : null;

            int iMax = MaxIterations;

            while (iMax > 0 && MoveNext())
            {
                iMax--;
                XNode    itemText = Current as XNode;
                XElement item     = itemText as XElement;
                if (item == null)
                {
                    if (itemText != null && itemText.Parent == root)
                    {
                        item = new XElement("span", itemText);
                    }
                    if (item == null || string.IsNullOrWhiteSpace(item.ToString()))
                    {
                        continue;
                    }
                }
                bool accept = parse(item);
                if (accept)
                {
                    if (!item.Equals(elBefore))
                    {
                        yield return(item);
                    }
                }
                else
                {
                    var children = item.DescendantNodes().GetEnumerator();
                    while (iMax > 0 && children.MoveNext())
                    {
                        iMax--;
                        XNode    itemChText = children.Current;
                        XElement itemCh     = itemChText as XElement;
                        if (itemCh == null)
                        {
                            if (itemChText != null && !string.IsNullOrWhiteSpace(itemChText.ToString()))
                            {
                                yield return(itemChText);
                            }
                            continue;
                        }
                        bool acceptCh = parse(itemCh);
                        if (acceptCh)
                        {
                            if (!itemCh.Equals(elBefore))
                            {
                                yield return(itemCh);
                            }
                            else
                            {
                                // todo
                            }
                        }
                        else
                        {
                            using (var numerator = new XEnumeratorElement(itemCh))
                            {
                                foreach (var el in numerator.DescendantsParse(item, parse)) // recursion
                                {
                                    if (!el.Equals(elBefore))
                                    {
                                        yield return(el);
                                    }
                                    else
                                    {
                                        // todo
                                    }
                                }
                            }
                        }

                        XEnumeratorElement.MoveAfter(children, itemChText);
                    }
                }

                MoveAfter(itemText);
            }
        }