Csharp/CSharp Tutorial/XML/XPathDocument

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Create XPathDocument from xml file

using System;
using System.Data;
using System.Xml;
using System.Xml.XPath;
public class MainClass  {
  public static void Main() {
      XPathDocument doc = new XPathDocument( "sample.xml" );
      XPathNavigator nav = doc.CreateNavigator();
      XPathExpression xpe = nav.rupile( "//name" );
      XPathNodeIterator ni = nav.Select( xpe );
  
      while ( ni.MoveNext() ) {
          Console.WriteLine(ni.Current.Value);
      }
  }
}

Navigate Xml with XPathNavigator

using System;
using System.Collections.Generic;
using System.ruponentModel;
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());
            }

        }
    }