Csharp/CSharp Tutorial/XML/XmlPathNavigator

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

Create XPathNavigator object by calling CreateNavigator of XmlDocument

using System;
using System.Xml.XPath;
using System.Xml;
class MainClass
{
  static void Main(string[] args)
    {
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(@"c:\books.xml");
    
        XPathNavigator nav = xmlDoc.CreateNavigator();
        Console.WriteLine("Author First Name");
        XPathNodeIterator itrator = nav.Select("descendant::first-name");         
        while( itrator.MoveNext() )
        {
            Console.WriteLine(itrator.Current.Value.ToString());
        }
    }
}

Get Node information by using XPathNavigator

using System;
using System.Xml.XPath;
using System.Xml;
class MainClass
{
  static void Main(string[] args)
    {
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(@"c:\books.xml");
    
        XPathNavigator nav = xmlDoc.CreateNavigator();
        nav.MoveToRoot();
        string name = nav.Name;
        Console.WriteLine("Root node info: ");
        Console.WriteLine("Base URI" + nav.BaseURI.ToString());
        Console.WriteLine("Name: " +nav.Name.ToString());
        Console.WriteLine("Node Type: "+ nav.NodeType.ToString());
        Console.WriteLine("Node Value: "+nav.Value.ToString());
    
        if (nav.HasChildren)
        {
            nav.MoveToFirstChild();
            GetNodeInfo(nav);
        }
    }
    private static void GetNodeInfo( XPathNavigator nav1)
    {
        Console.WriteLine("Name: " +nav1.Name.ToString());
        Console.WriteLine("Node Type: "+ nav1.NodeType.ToString());
        Console.WriteLine("Node Value: "+nav1.Value.ToString());
    
        if (nav1.HasChildren)
        {
            nav1.MoveToFirstChild();
            
            while( nav1.MoveToNext() )
            {
                GetNodeInfo(nav1);
                nav1.MoveToParent();
            }               
        } else {   
            nav1.MoveToNext();
            GetNodeInfo(nav1);
        }
    }
}

Select a node

using System;
using System.Xml.XPath;
using System.Xml;
class MainClass
{
  static void Main(string[] args)
    {
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(@"c:\books.xml");
    
        XPathNavigator nav = xmlDoc.CreateNavigator();
        Console.WriteLine("Author First Name");
        XPathNodeIterator itrator = nav.Select("descendant::first-name");         
        while( itrator.MoveNext() )
        {
            Console.WriteLine(itrator.Current.Value.ToString());
        }
    }
}

XPathNavigator: Evaluate

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.XPath;
class MainClass
{
    static void Main(string[] args)
    {
        string xml = "<Order>" +
                        "<Item>" +
                            "<Description>Some widget part</Description>" +
                            "<Price>12.99</Price>" +
                        "</Item>" +
                        "<Item>" +
                            "<Description>Another widget</Description>" +
                            "<Price>50.12</Price>" +
                        "</Item>" +
                    "</Order>";
        XmlDocument doc = new XmlDocument();
        doc.LoadXml(xml);
        XPathNavigator nav = doc.CreateNavigator();
        Console.WriteLine("Total price for this order is ${0}", nav.Evaluate("sum(Order/Item/Price)"));
    }
}
Total price for this order is $63.11

XPathNavigator: XPathNodeIterator with comparison

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.XPath;
class MainClass
{
    static void Main(string[] args)
    {
        string xml = "<Order>" +
                        "<Item>" +
                            "<Description>widget part</Description>" +
                            "<Price>1.9</Price>" +
                        "</Item>" +
                        "<Item>" +
                            "<Description>Another widget</Description>" +
                            "<Price>5.2</Price>" +
                        "</Item>" +
                    "</Order>";
        XmlDocument doc = new XmlDocument();
        doc.LoadXml(xml);
        XPathNavigator nav = doc.CreateNavigator();
        XPathNodeIterator nodes = nav.Select("/Order/Item[Price>10]/Description");
        while (nodes.MoveNext())
        {
            Console.WriteLine("Item {0} has a price greater than 10",nodes.Current.Value);
        }
    }
}

XPathNodeIterator from a select-node statement

using System;
using System.Xml.XPath;
using System.Xml;
class MainClass
{
  static void Main(string[] args)
    {
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(@"c:\books.xml");
    
        XPathNavigator nav = xmlDoc.CreateNavigator();
        Console.WriteLine("Author First Name");
        XPathNodeIterator itrator = nav.Select("descendant::first-name");         
        while( itrator.MoveNext() )
        {
            Console.WriteLine(itrator.Current.Value.ToString());
        }
    }
}