using System; using System.Xml.XPath; class XPathNavigatorSample { [STAThread] static void Main(string[] args) { XPathDocument xpathDocument = new XPathDocument("../../sample.xml"); XPathNavigator xpathNavigator = xpathDocument.CreateNavigator(); xpathNavigator.MoveToRoot(); NavigateTree(xpathNavigator); } private static void NavigateTree (XPathNavigator xpathNavigator) { if (xpathNavigator.HasChildren) { xpathNavigator.MoveToFirstChild(); DisplayNode(xpathNavigator); NavigateTree(xpathNavigator); while (xpathNavigator.MoveToNext()) { DisplayNode(xpathNavigator); NavigateTree(xpathNavigator); } xpathNavigator.MoveToParent(); } } private static void DisplayNode (XPathNavigator xpathNavigator) { if (xpathNavigator.NodeType == XPathNodeType.Text) { Console.Out.WriteLine(xpathNavigator.Value); } else { Console.Out.Write(xpathNavigator.Name); if (xpathNavigator.HasAttributes) { int attributesCount = 1; while (xpathNavigator.MoveToNextAttribute()) { attributesCount ++; } Console.Out.Write(attributesCount); if (attributesCount != 1) { xpathNavigator.MoveToParent(); } } } } }