Mega Code Archive

 
Categories / C# Tutorial / XML
 

Navigate Xml with XPathNavigator

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Xml; using System.Xml.XPath;     public class MainClass     {         public static void Main()         {         XmlDocument doc = new XmlDocument();         XPathNavigator navigator = null;             doc.Load(Application.StartupPath + @"\employees.xml");             navigator = doc.CreateNavigator();                          navigator.MoveToRoot();             navigator.MoveToFirstChild();             while (navigator.MoveToNext())             {                 navigator.MoveToFirstChild();                 do                 {                     string id = navigator.GetAttribute("employeeid", "");                     if (id == "1")                     {                         navigator.MoveToFirstChild();                         do                         {                             switch (navigator.Name)                             {                                 case "firstname":                                     Console.WriteLine(navigator.Value);                                     break;                                 case "lastname":                                     Console.WriteLine(navigator.Value);                                     break;                                 case "homephone":                                     Console.WriteLine(navigator.Value);                                     break;                                 case "notes":                                     Console.WriteLine(navigator.Value);                                     break;                             }                         }                         while (navigator.MoveToNext());                         navigator.MoveToParent();                     }                 }                 while (navigator.MoveToNext());             }                            navigator.MoveToRoot();             navigator.MoveToFirstChild();             while (navigator.MoveToNext())             {                 navigator.MoveToFirstChild();                 do                 {                     string id = navigator.GetAttribute("employeeid", "");                     if (id == "2")                     {                         navigator.MoveToFirstChild();                         do                         {                             switch (navigator.Name)                             {                                 case "firstname":                                     navigator.SetValue("2");                                     break;                                 case "lastname":                                     navigator.SetValue("1");                                     break;                                 case "homephone":                                     navigator.SetValue("3");                                     break;                                 case "notes":                                     navigator.SetValue("4");                                     break;                             }                         }                         while (navigator.MoveToNext());                         navigator.MoveToParent();                     }                 }                 while (navigator.MoveToNext());             }         }     }