CSDN博客

img geekone

System.Xml.XPath的XPathNavigator(1)

发表于2004/10/8 10:29:00  651人阅读

分类: 4-XML

XPathNavigator 类是 XPathDocumnet里的一个子类。dotnetFx  V1,V2都有。XPathDocument在V2版本多出了许多新的类和方法,更加强大了XPath并且XQuery,所以先理出相同部分与V1版本的。

static void Main(string[] args)
        {
            //xpath object load xml file
             XPathDocument xpDoc = new XPathDocument("booksort.xml");
            //create a XPathNavigator object
            XPathNavigator xpNav = xpDoc.CreateNavigator();
           
           //XPathExpression 是集成的XPath表达示,如果需要排序   
           /*
             XPathExpression xpExpr = xpNav.Compile("descendant::book[author/last-name='Austen']");
            xpExpr.AddSort("title", XmlSortOrder.Descending, XmlCaseOrder.None, "", XmlDataType.Text);
            XPathNodeIterator xpNi = xpNav.Select(xpExpr);
           */
         
            //不需要排序,就直接把XPath查询的节点代入一个XPathNodeIterator 节点
            XPathNodeIterator xpNi = xpNav.Select("descendant::book[author/last-name='Austen']");
            //操作XPathNodeIterator
            while (xpNi.MoveNext())
            {
                //最好用Clone方法复自一个XPathNavigator 方便操作
                //XPathNavigator clone = xpNi.Current.Clone();
                //clone.MoveToFirstChild();
                //Console.WriteLine("{0}:{1}", clone.Name,clone.Value);
                xpNi.Current.MoveToFirstChild();
                Console.WriteLine("{0},{1}", xpNi.Current.Name, xpNi.Current.Value);
                Console.ReadLine();
            }
}


这在V1,V2中都通过,两个版本都有上面的属性和方法。

阅读全文
0 0

相关文章推荐

img
取 消
img