📄 pathnavigator.cs
字号:
this.Text = "PathNavigator";
this.navigateBox.ResumeLayout(false);
this.locateBox.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run( new PathNavigator() );
}
// traverse to first child
private void firstChildButton_Click( object sender,
System.EventArgs e )
{
TreeNode newTreeNode;
// move to first child
if ( xpath.MoveToFirstChild() )
{
newTreeNode = new TreeNode(); // create new node
// set node's Text property to either
// navigator's name or value
DetermineType( newTreeNode, xpath );
// add node to TreeNode node list
tree.Nodes.Add( newTreeNode );
tree = newTreeNode; // assign tree newTreeNode
// update TreeView control
pathTreeViewer.ExpandAll();
pathTreeViewer.Refresh();
pathTreeViewer.SelectedNode = tree;
}
else // node has no children
MessageBox.Show( "Current Node has no children.",
"", MessageBoxButtons.OK,
MessageBoxIcon.Information );
}
// traverse to node's parent on parentButton click event
private void parentButton_Click( object sender,
System.EventArgs e )
{
// move to parent
if ( xpath.MoveToParent() )
{
tree = tree.Parent;
// get number of child nodes, not including sub trees
int count = tree.GetNodeCount( false );
// remove all children
tree.Nodes.Clear();
// update TreeView control
pathTreeViewer.ExpandAll();
pathTreeViewer.Refresh();
pathTreeViewer.SelectedNode = tree;
}
else // if node has no parent (root node)
MessageBox.Show( "Current node has no parent.", "",
MessageBoxButtons.OK,
MessageBoxIcon.Information );
}
// find next sibling on nextButton click event
private void nextButton_Click( object sender,
System.EventArgs e )
{
TreeNode newTreeNode = null, newNode = null;
// move to next sibling
if ( xpath.MoveToNext() )
{
newTreeNode = tree.Parent; // get parent node
newNode = new TreeNode(); // create new node
DetermineType( newNode, xpath );
newTreeNode.Nodes.Add( newNode );
// set current position for display
tree = newNode;
// update TreeView control
pathTreeViewer.ExpandAll();
pathTreeViewer.Refresh();
pathTreeViewer.SelectedNode = tree;
}
else // node has no additional siblings
MessageBox.Show( "Current node is last sibling.",
"", MessageBoxButtons.OK,
MessageBoxIcon.Information );
} // end nextButton_Click
// get previous sibling on previousButton click
private void previousButton_Click( object sender,
System.EventArgs e )
{
TreeNode parentTreeNode = null;
// move to previous sibling
if ( xpath.MoveToPrevious() )
{
parentTreeNode = tree.Parent; // get parent node
// delete current node
parentTreeNode.Nodes.Remove( tree );
// move to previous node
tree = parentTreeNode.LastNode;
// update TreeView control
pathTreeViewer.ExpandAll();
pathTreeViewer.Refresh();
pathTreeViewer.SelectedNode = tree;
}
else // if current node has no previous siblings
MessageBox.Show( "Current node is first sibling.",
"", MessageBoxButtons.OK,
MessageBoxIcon.Information );
} // end previousButton_Click
// process selectButton click event
private void selectButton_Click( object sender,
System.EventArgs e )
{
XPathNodeIterator iterator; // enables node iteration
// get specified node from ComboBox
try
{
iterator = xpath.Select( selectComboBox.Text );
DisplayIterator( iterator ); // print selection
}
// catch invalid expressions
catch ( System.ArgumentException argumentException )
{
MessageBox.Show( argumentException.Message,
"Error", MessageBoxButtons.OK,
MessageBoxIcon.Error );
}
} // end selectButton_Click
// print values for XPathNodeIterator
private void DisplayIterator( XPathNodeIterator iterator )
{
selectTreeViewer.Text = "";
// prints selected node's values
while ( iterator.MoveNext() )
selectTreeViewer.Text +=
iterator.Current.Value.Trim()
+ "\r\n";
} // end DisplayIterator
// determine if TreeNode should display current node
// name or value
private void DetermineType( TreeNode node,
XPathNavigator xPath )
{
// determine NodeType
switch ( xPath.NodeType )
{
// if Element, get its name
case XPathNodeType.Element:
// get current node name, and remove whitespace
node.Text = xPath.Name.Trim();
break;
// obtain node values
default:
// get current node value and remove whitespace
node.Text = xPath.Value.Trim();
break;
} // end switch
} // end DetermineType
} // end PathNavigator
/*
**************************************************************************
* (C) Copyright 2002 by Deitel & Associates, Inc. and Prentice Hall. *
* All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
**************************************************************************
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -