internal XPathContext Map(XmlNode n) { XPathContext ctx = (XPathContext)xpathContext.Clone(); ctx.NavigateToChild(childIndex[n]); return(ctx); }
public void ShouldCreateCopyOnClone() { List <Element> l = new List <Element>(); l.Add(new Element("foo")); l.Add(new Element("foo")); l.Add(new Element("bar")); XPathContext ctx = new XPathContext(); ctx.SetChildren(l); ctx.NavigateToChild(1); Assert.AreEqual("/foo[2]", ctx.XPath); Assert.AreEqual("/", ctx.ParentXPath); XPathContext clone = (XPathContext)ctx.Clone(); Assert.AreEqual("/foo[2]", clone.XPath); Assert.AreEqual("/", clone.ParentXPath); Assert.AreNotSame(ctx, clone); clone.NavigateToParent(); clone.NavigateToChild(2); Assert.AreEqual("/bar[1]", clone.XPath); Assert.AreEqual("/", clone.ParentXPath); Assert.AreEqual("/foo[2]", ctx.XPath); Assert.AreEqual("/", ctx.ParentXPath); }
/// <summary> /// Creates an instance of ChildNodeXPathContextProvider. /// </summary> /// <param name="parentContext">parentContext context of the /// parent of all Nodes ever expected to be passed in as /// arguments to Map. This XPathContext must be "positioned /// at" the parent element and already know about all its /// children.</param> /// <param name="children">all child nodes of the parent in /// the same order they are known to the XPathContext.</param> internal ChildNodeXPathContextProvider(XPathContext parentContext, IEnumerable <XmlNode> children) { this.xpathContext = (XPathContext)parentContext.Clone(); childIndex = children.Select((n, i) => new { Node = n, Index = i }) .ToDictionary(t => t.Node, t => t.Index); }