/// <summary> /// Evaluates the XPath expression and returns the typed result. /// </summary> /// <param name="xpath">A string representing an XPath expression that can be evaluated.</param> /// <returns></returns> public object SelectXmlNodes(string xpath) { object retval = null; // get the selected node XPathNavigatorTreeNode node = this.SelectedNode as XPathNavigatorTreeNode; // if there is no selected node, default to the root node if (node == null && this.Nodes.Count > 0) { node = this.GetRootXmlTreeNode(); } if (node == null) { return(null); } try { XPathExpression xpe = node.Navigator.Compile(xpath); retval = node.Navigator.Evaluate(xpe); } catch (Exception ex) { ExpressionResultsWindow dialog = new ExpressionResultsWindow(); dialog.Expression = xpath; dialog.Result = ex.Message; dialog.ShowDialog(this.FindForm()); logger.Error(SQLSchemaTool.ERRORFORMAT, ex.Message, ex.Source, ex.StackTrace); } // evaluate the expression, return the result return(retval); }
/// <summary> /// Loads an XPathNavigatorTreeNode for each XPathNavigator in the specified XPathNodeIterator, into the /// specified TreeNodeCollection. /// </summary> /// <param name="iterator"></param> /// <param name="treeNodeCollection"></param> public virtual void LoadNodes(XPathNodeIterator iterator, TreeNodeCollection treeNodeCollection) { // handle null arguments if (iterator == null) { throw new ArgumentNullException("navigator"); } if (treeNodeCollection == null) { throw new ArgumentNullException("parentNodeCollection"); } // use the wait cursor, in case this takes a while this.UseWaitCursor = true; try { treeNodeCollection.Clear(); // create and add a node for each navigator foreach (XPathNavigator navigator in iterator) { XPathNavigatorTreeNode node = new XPathNavigatorTreeNode(navigator.Clone()); treeNodeCollection.Add(node); } } finally { this.UseWaitCursor = false; } }
/// <summary> /// Handles the expanding of an XPathNavigatorTreeNode. /// </summary> /// <param name="node"></param> protected virtual void ExpandNode(XPathNavigatorTreeNode treeNode) { if (treeNode == null) { return; } // for better performance opening large files, tree nodes are loaded on demand. // return if the node has already been expanded and loaded if (treeNode.HasExpanded) { return; } // make sure we don't have to load this tree node again. treeNode.HasExpanded = true; this.BeginUpdate(); try { // load the child nodes of the specified xml tree node this.LoadNodes(treeNode.Navigator.SelectChildren(XPathNodeType.All), treeNode.Nodes); } finally { this.EndUpdate(); } }
/// <summary> /// Finds a TreeNode for a given stack of XPathNavigators. /// </summary> /// <param name="ancestors">A Stack of XPathNavigators with which to find a TreeNode.</param> /// <returns></returns> private TreeNode GetXmlTreeNode(Stack <XPathNavigator> ancestors) { XPathNavigator navigator = null; // start at the root TreeNodeCollection nodes = this.Nodes; TreeNode treeNode = null; // loop through the ancestor XPathNavigators while (ancestors.Count > 0 && (navigator = ancestors.Pop()) != null) { // loop through the TreeNodes at the current level foreach (TreeNode node in nodes) { // make sure it's an XPathNavigatorTreeNode XPathNavigatorTreeNode xmlTreeNode = node as XPathNavigatorTreeNode; if (xmlTreeNode == null) { continue; } // check to see if we've found the correct TreeNode if (xmlTreeNode.Navigator.IsSamePosition(navigator)) { // expand the tree node, if it hasn't alreay been expanded if (!node.IsExpanded) { node.Expand(); } // we've taken another step towards the target node treeNode = node; // update the current level nodes = node.Nodes; // handle the next level, if any break; } } } // return the result, if any was found return(treeNode); }
/// <summary> /// Returns the root XPathNavigatorTreeNode in the tree. /// </summary> /// <returns></returns> private XPathNavigatorTreeNode GetRootXmlTreeNode() { foreach (TreeNode node in this.Nodes) { XPathNavigatorTreeNode xmlTreeNode = node as XPathNavigatorTreeNode; if (xmlTreeNode == null || xmlTreeNode.Navigator == null) { continue; } if (xmlTreeNode.Navigator.NodeType != XPathNodeType.Element) { continue; } return(xmlTreeNode); } return(this.Nodes[0] as XPathNavigatorTreeNode); }
/// <summary> /// Loads the initial xml tree node. /// </summary> protected virtual void Initialize() { XPathNavigatorTreeNode node = null; // suspend drawing of the tree while loading nodes to improve performance // as adding each node would normally require an entire redraw of the tree base.BeginUpdate(); try { // clear all the nodes in the tree base.Nodes.Clear(); // setting the navigator to null should just clear the tree // no exception needs thrown if (_navigator != null) { // if it's the root node of the document, load it's children // we don't display the root if (_navigator.NodeType == XPathNodeType.Root) { this.LoadNodes(_navigator.SelectChildren(XPathNodeType.Element), base.Nodes); } else { // otherwise, create a tree node and load it node = new XPathNavigatorTreeNode(_navigator); base.Nodes.Add(node); } } } finally { // resume drawing of the tree base.EndUpdate(); if (node != null) { node.Expand(); } } }
protected override void OnBeforeExpand(TreeViewCancelEventArgs e) { XPathNavigatorTreeNode node = e.Node as XPathNavigatorTreeNode; // expand the node, adding any child xml tree nodes this.ExpandNode(node); if (node != null) { if (node.Nodes.Count > 0) { TreeNodeCollection nodes = base.Nodes; if (node.Parent != null) { nodes = node.Parent.Nodes; } // add a node for the xml end tag, such as </node> node.Tag = nodes.Insert(e.Node.Index + 1, string.Format("</{0}>", node.Navigator.Name)); } } base.OnBeforeExpand(e); }
/// <summary> /// Loads the initial xml tree node. /// </summary> protected virtual void Initialize() { XPathNavigatorTreeNode node = null; // suspend drawing of the tree while loading nodes to improve performance // as adding each node would normally require an entire redraw of the tree base.BeginUpdate(); try { // clear all the nodes in the tree base.Nodes.Clear(); // setting the navigator to null should just clear the tree // no exception needs thrown if (_navigator != null) { // if it's the root node of the document, load it's children // we don't display the root if (_navigator.NodeType == XPathNodeType.Root) { this.LoadNodes(_navigator.SelectChildren(XPathNodeType.Element), base.Nodes); } else { // otherwise, create a tree node and load it node = new XPathNavigatorTreeNode(_navigator); base.Nodes.Add(node); } } } finally { // resume drawing of the tree base.EndUpdate(); if (node != null) { node.Expand(); } } }
/// <summary> /// Loads an XPathNavigatorTreeNode for each XPathNavigator in the specified XPathNodeIterator, into the /// specified TreeNodeCollection. /// </summary> /// <param name="iterator"></param> /// <param name="treeNodeCollection"></param> public virtual void LoadNodes(XPathNodeIterator iterator, TreeNodeCollection treeNodeCollection) { // handle null arguments if (iterator == null) throw new ArgumentNullException("navigator"); if (treeNodeCollection == null) throw new ArgumentNullException("parentNodeCollection"); // use the wait cursor, in case this takes a while this.UseWaitCursor = true; try { treeNodeCollection.Clear(); // create and add a node for each navigator foreach (XPathNavigator navigator in iterator) { XPathNavigatorTreeNode node = new XPathNavigatorTreeNode(navigator.Clone()); treeNodeCollection.Add(node); } } finally { this.UseWaitCursor = false; } }
/// <summary> /// Handles the expanding of an XPathNavigatorTreeNode. /// </summary> /// <param name="node"></param> protected virtual void ExpandNode(XPathNavigatorTreeNode treeNode) { if (treeNode == null) return; // for better performance opening large files, tree nodes are loaded on demand. // return if the node has already been expanded and loaded if (treeNode.HasExpanded) return; // make sure we don't have to load this tree node again. treeNode.HasExpanded = true; this.BeginUpdate(); try { // load the child nodes of the specified xml tree node this.LoadNodes(treeNode.Navigator.SelectChildren(XPathNodeType.All), treeNode.Nodes); } finally { this.EndUpdate(); } }