C#中使用XPath的XPathNavigator类移动选择XML数据

    技术2022-05-20  58

    XPathNavigator类包含移动和选择XML所需元素的所有方法。 1.创建:   a.如果是从XPathDocument中创建,则是只读的,只能浏览数据;   b.如果是从XmlDocument中创建,则可以编辑文档; 2.查询:使用Select()方法通过XPath语句查询; 3.迭代:使用XPathNodeIterator类,MoveNext()方法移动下一个节点;Current属性表示当前节点; 4.求值:使用Evaluate()方法计算表达式的值; 5.插入节点:先检查CanEdit属性是否为true,再使用InsertAfter()方法插入新节点。 实例:查询和编辑books.xml

     

    01. using  System; 02. using  System.Collections.Generic; 03. using  System.Text; 04. using  System.Xml.XPath; 05. using  System.Xml; 06.   07. namespace  Magci.Test.XML.TestXPath 08. { 09.      class  Program 10.      { 11.          private  static  XPathDocument doc; 12.          private  static  XPathNavigator nav; 13.   14.          static  void  Main( string [] args) 15.          { 16.              doc =  new  XPathDocument(@ "../../books.xml" ); 17.              nav = ((IXPathNavigable)doc).CreateNavigator(); 18.              SearchByGenre( "nove" ); 19.              TotalPrice(); 20.              Insert(); 21.              Console.ReadLine(); 22.          } 23.   24.          public  static  void  SearchByGenre( string  search) 25.          { 26.              //查询bookstore的子元素book中genre属性值为novel的所有节点 27.              XPathNodeIterator iter = nav.Select( "/bookstore/book[@genre='"  + search +  "l']" ); 28.   29.              Console.WriteLine( "/nSearchByGenre:/n" ); 30.              while  (iter.MoveNext()) 31.              { 32.                  //迭代所有子代节点 33.                  XPathNodeIterator newIter = iter.Current.SelectDescendants(XPathNodeType.Element,  false ); 34.                  while  (newIter.MoveNext()) 35.                  { 36.                      Console.WriteLine(newIter.Current.Name +  " : "  + newIter.Current.Value); 37.                  } 38.              } 39.          } 40.   41.          public  static  void  TotalPrice() 42.          { 43.              XPathNodeIterator iter = nav.Select( "/bookstore/book" ); 44.              Console.WriteLine( "/nTotalPrice:/n" ); 45.              while  (iter.MoveNext()) 46.              { 47.                  XPathNodeIterator newIter = iter.Current.SelectDescendants(XPathNodeType.Element,  false ); 48.                  while  (newIter.MoveNext()) 49.                  { 50.                      if  (newIter.Current.Name ==  "price" ) 51.                      { 52.                          Console.WriteLine(newIter.Current.Name +  " : "  + newIter.Current.Value); 53.                      } 54.                  } 55.              } 56.              //统计图书总价 57.              Console.WriteLine( "Total price = {0}" , nav.Evaluate( "sum(bookstore/book/price)" )); 58.          } 59.   60.          //插入节点 61.          public  static  void  Insert() 62.          { 63.              XmlDocument doc =  new  XmlDocument(); 64.              doc.Load(@ "../../books.xml" ); 65.              XPathNavigator nav2 = doc.CreateNavigator(); 66.              //判断是否可编辑 67.              if  (nav2.CanEdit) 68.              { 69.                  XPathNodeIterator iter = nav2.Select( "bookstore/book/price" ); 70.                  while  (iter.MoveNext()) 71.                  { 72.                      //在当前节点之后插入新节点 73.                      iter.Current.InsertAfter( "<disc>5</disc>" ); 74.                  } 75.              } 76.              doc.Save(@ "../../newBooks.xml" ); 77.              Console.WriteLine( "/nnewBooks.xml saved successful./n" ); 78.          } 79.      } 80. } book.xml: 01. <? xml  version = "1.0"  encoding = "utf-8"  ?> 02. < bookstore > 03.    < book  genre = "autobiography"  publicationdate = "1991"  ISBN = "1-861003-11-0" > 04.      < title >The Autobiography of Benjamin Franklin</ title > 05.      < author > 06.        < first-name >Benjamin</ first-name > 07.        < last-name >Franklin</ last-name > 08.      </ author > 09.      < price >8.99</ price > 10.    </ book > 11.    < book  genre = "novel"  publicationdate = "1967"  ISBN = "0-201-63361-2" > 12.      < title >The Confidence Man</ title > 13.      < author > 14.        < first-name >Herman</ first-name > 15.        < last-name >Melville</ last-name > 16.      </ author > 17.      < price >11.99</ price > 18.    </ book > 19.    < book  genre = "philosophy"  publicationdate = "1991"  ISBN = "1-861001-57-6" > 20.      < title >The Gorgias</ title > 21.      < author > 22.        < name >Plato</ name > 23.      </ author > 24.      < price >9.99</ price > 25.    </ book > 26. </ bookstore > newBooks.xml:编辑后的文档 01. <? xml  version = "1.0"  encoding = "utf-8" ?> 02. < bookstore > 03.    < book  genre = "autobiography"  publicationdate = "1991"  ISBN = "1-861003-11-0" > 04.      < title >The Autobiography of Benjamin Franklin</ title > 05.      < author > 06.        < first-name >Benjamin</ first-name > 07.        < last-name >Franklin</ last-name > 08.      </ author > 09.      < price >8.99</ price > 10.      < disc >5</ disc > 11.    </ book > 12.    < book  genre = "novel"  publicationdate = "1967"  ISBN = "0-201-63361-2" > 13.      < title >The Confidence Man</ title > 14.      < author > 15.        < first-name >Herman</ first-name > 16.        < last-name >Melville</ last-name > 17.      </ author > 18.      < price >11.99</ price > 19.      < disc >5</ disc > 20.    </ book > 21.    < book  genre = "philosophy"  publicationdate = "1991"  ISBN = "1-861001-57-6" > 22.      < title >The Gorgias</ title > 23.      < author > 24.        < name >Plato</ name > 25.      </ author > 26.      < price >9.99</ price > 27.      < disc >5</ disc > 28.    </ book > 29. </ bookstore >

    最新回复(0)