HOW TO: Execute XPath Queries by Using the System.Xml.XPath Classes

???? ?????? ???? ??????
???? ID: 317069 - ?? ???????? ?? ?????? ??? ?? ?? ???? ???? ???? ??.
??? ?? ??????? ???? | ??? ?? ??????? ????

?? ????? ??

??????

This step-by-step article demonstrates how to use the classes in theSystem.Xml.XPathnamespace to execute XPath queries in .NET applications.

Explore the System.Xml.XPath Namespace

TheSystem.Xml.XPathnamespace contains the XPath parser and the evaluation engine of the .NET Framework. TheSystem.Xml.XPathnamespace supports the World Wide Web Consortium (W3C) XML Path Language (XPath) Version 1.0 Recommendation. For more information about the XPath recommendation, visit the following W3C Web site:
XML Path Language (XPath) Version 1.0
TheSystem.Xml.XPathnamespace also contains the following classes that are optimized to execute XPath queries and navigate the generated resultsets programmatically:
  • XPathDocument: A high performance, read-only cache for processing XML documents and streams.XPathDocumentis optimized for Extensible Stylesheet Language Transformation (XSLT) processing and for the XPath data model.XPathDocumentuses the document object model (DOM)-based parsing model (loads the source XML into an in-memory tree structure) but does not implement the interfaces required to insert, to modify, or to delete nodes in an Extensible Markup Language (XML) document or stream.
  • XPathNavigator: ???? ?? XPath ???? ???? ?? ?????? ?? ???XPathNavigatorXML ???? ?? ??? ??? ???? ?? ??? XPath ?????? ?? ????????? ???? ?? ??? ?????? ??????? ?????? ????? ?? ??XPathDocument.XPathNavigator?? ????? ?? ???? ??CreateNavigator?? ?????? ??XPathDocument???????? ??? ?? ????? ?? ????XPathNavigator????? executed XPath ?????? ????????????? ?????? ???? ?? ????? ???? ?? ??? ???System.Xml.XPath.XPathExpression???????? ?? ?????? ?????? encapsulates.
  • XPathNodeIterator: ???? ???????? ????, ' ????-????? ?? ??? iterator ?????? ????? ??? ?? ?? ????? ??????? ?????? ?????? ??XPathNavigator???????? ???? XPath ??????, ?? ??????? ?? ????????? ???? ?? ???XPathNodeIterator????? ??? ??? ?? ????? ?? ???? ???XPathNodeIteratorXPath ?????? ?????? ??????? ?? ?? ?? ?? resultset ??????? ???? ???? ?? ????
  • XPathExpression: ?????? XPath ?????? ?????? encapsulates ??? You must compile an XPath query expression before you can execute the query expression. A compiled XPath expression contains syntax that is verified to conform to the W3C XPath Query Language specification. A call to the?????? ????method of anXPathNavigatorobject returns theXPathExpression????? Typically, you useXPathExpressionto supply a precompiled XPath query expression in a call to the??? ????method of anXPathNavigator???????? ???
  • XPathException: The .NET Framework exception that is generated when an error occurs while processing an XPath query expression. The??? ???????? ???????? ?????? ??? ????XPathNavigatorclass can raiseXPathException.

Create Sample XML Documents

????? ?? ??? ?? ????? XML ???????? ?????? .NET ??? ????? ?? ??? ????? ???? ?? ???, ????? ????? ?? ???? ????:
  1. ??? ??? XML ???????? ??? ????? ??? ??????? ???? ?? ??? ????? ?????? ?? ???? ??? ?????? Books.xml ???? ???? ????? ????? ?? ??? ?????? ??? ???????? ???????
    <?xml version='1.0'?>
    <Books>
    <Book>
      <Title>Beginning XML</Title>
      <Publisher>Wrox</Publisher>
    </Book>
    <Book>
      <Title>XML Step by Step</Title>
      <Publisher>MSPress</Publisher>
    </Book>
    <Book>
      <Title>Professional XML</Title>
      <Publisher>Wrox</Publisher>
    </Book>
    <Book>
      <Title>Developing XML solutions</Title>
      <Publisher>MSPress</Publisher>
    </Book>
    </Books>
    					
  2. ??? ??? XML ???????? ??? ????? ??? ??????? ???? ?? ??? ????? ?????? ?? ???? ??? ?????? DotNetBooks.xml ???? ???? ????? ????? ?? ??? ?????? ??? ???????? ???????
    <?xml version='1.0'?>
    <DotNetBooks>
    <DotNetBook>
      <Title>Professional C#</Title>
      <Publisher>Wrox</Publisher>
    </DotNetBook>
    <DotNetBook>
      <Title>ADO.Net Core Reference</Title>
      <Publisher>MSPress</Publisher>
    </DotNetBook>
    <DotNetBook>
      <Title>Professional VB.Net</Title>
      <Publisher>Wrox</Publisher>
    </DotNetBook>
    <DotNetBook>
      <Title>Inside C#</Title>
      <Publisher>MSPress</Publisher>
    </DotNetBook>
    </DotNetBooks>

???? Visual Basic .NET ????????? ?????

  1. Visual Studio .NET ??? ??????? ????, ?? ???? ??? ?? ??? .NET Windows ????????? Visual Basic ????????? ??????
  2. ?? ????????????????? ????? ????? ?? Form1.vb ???? ?? ????
  3. ??? ????? ??? ???????Click_Event????????? ?? ???? ???? ??? ??:
    'Instantiate the XPathDocument class.
    Dim xmldoc As New System.Xml.XPath.XPathDocument("c:\books.xml")
    
    'Instantiate the XPathNavigator class.
    Dim nav As System.Xml.XPath.XPathNavigator = xmldoc.CreateNavigator()
    
    'Instantiate the XPathIterator class.
    Dim iterator As System.Xml.XPath.XPathNodeIterator = nav.Select("//Publisher[. = 'MSPress']/parent::node()/Title")
    
    'Use the XPathIterator class to navigate through the generated resultset
    'and then display the selected Titles.
    Do While iterator.MoveNext
        System.Diagnostics.Debug.WriteLine(iterator.Current.Value)
    Loop
    					
  4. ??? ?? ??????????? ?? ????? ?? ??? ?????? ?????????? ?????? ???? ??? instantiates ?? ?????????? ?? ???????? ?? ???? ??? ???????? ?? ????? ???? ?? ??? ????? ????? ?????? ????System.Xml.XPath??? ????? ??? ??? ???? ?? ??? ???????? XPath ?????? ?????? ???????? ??? ????????? ???? ?? ??? ???? ?? ?????? ?????? ??? ????XPathNavigator???????? ???
  5. ??? ????? ??? ???????Click_Event????????? ?? ????? ???? ??? ??:
    'Construct the XPathDocument by specifying the path to Books.xml.
    Dim xmldoc As New System.Xml.XPath.XPathDocument("c:\books.xml")
    
    'Create the XPathNavigator.
    Dim nav As System.Xml.XPath.XPathNavigator = xmldoc.CreateNavigator()
    
    'Compile the XPath query expression to identify all MSPress Titles.
    'The Compile method of the XPathNavigator generates an XPathExpression object
    'that encapsulates the compiled query expression.
    
    Dim expr As System.Xml.XPath.XPathExpression = nav.Compile("//Publisher[. = 'MSPress']/parent::node()/Title")
    
    'Execute the Select method of the XPathNavigator to create the XPathNodeIterator.
    'Note that the sample code supplies the XPathExpression object as the query expression parameter.
    Dim iterator As System.Xml.XPath.XPathNodeIterator = nav.Select(expr)
    
    System.Diagnostics.Debug.WriteLine("MSPress titles in books.xml...")
    System.Diagnostics.Debug.WriteLine("******************************")
    
    'Use the iterator to navigate the generated resultset.
    Do While iterator.MoveNext
        System.Diagnostics.Debug.WriteLine(iterator.Current.Value)
    Loop
    
    
    'Release the XPathDocument and XPathNavigator.
    xmldoc = Nothing
    nav = Nothing
    
    System.Diagnostics.Debug.WriteLine(vbCrLf & "MSPress titles in DotNetBooks.xml...")
    System.Diagnostics.Debug.WriteLine("******************************")
    
    'Specify the path to DotNetBooks.xml to reconstruct the XPathDocument.
    xmldoc = New System.Xml.XPath.XPathDocument("c:\DotNetBooks.xml")
    
    'Create the XPathNavigator.
    nav = xmldoc.CreateNavigator()
    
    'Reuse the XPathExpression that you compiled previously to locate
    'all MSPress titles in DotNetBooks.xml.
    
    'You can reuse the XPathExpression because the hierarchy of elements that are referenced
    'in the XPath query is the same in Books.xml and DotNetBooks.xml. 
    'For example, the Publisher element is a child of the Title element in both XML documents.
    'The hierarchy must be identical for you to reuse the same XPathExpression object
    'across different XML documents/streams.
    
    'When you use an XPathExpression object, and the Select method executes,
    'the XPath query is not recompiled. 
    
    iterator = nav.Select(expr)
    
    'Use the iterator to navigate the generated resultset.
    Do While iterator.MoveNext
         System.Diagnostics.Debug.WriteLine(iterator.Current.Value)
    Loop
    					
  6. ??? ?? ??????????? ?? ????? ?? ??? ?????? ?????????? ?????? ???? instantiates ????? ??? ?? ??? ????? ????? ?????? ????XPathExpression(?? Books.xml), ???? ??? ???????? ?? ???? ??? ?? XPath ?????? ????????? ???? ?? (DotNetBooks.xml) ?? ????? ??? ???????? reuses.

????????? ?? ??????? ????

  1. Visual Basic .NET ????????? ?? ??? ???????? ?????? ?? ????????? ?? ????????? ?????
  2. ?? ??????? ????? ???? ??, ?? XPath ?????? ?????? ?? ????????? ???? ?? ??? ??? ???????? ?? ????? ???? ?? ?? ??? ?? ????????? ???? ?? ??? ???? ???? ??? ????? ????? ?????? Books.xml ?? ??????? ????????? XPath ?????? ?? ??? ??? ????? ???? ??:
    XML Step by Step
    Developing XML solutions
    					
  3. ??? ?? ????? ???? ?? ?? ?? ????????? ???? ?? ??? ????? ???? ??? ????? ???? ???XPathExpressionXPath ?????? ?????? ?? ????????? ???? ?? ??? ???????? ??? ?????? Books.xml ?? DotNetBooks.xml ????????? XPath ?????? ?? ??? ??? ????? ???? ??:
    MSPress titles in books.xml...
    ******************************
    XML Step by Step
    Developing XML solutions
    
    MSPress titles in DotNetBooks.xml...
    ******************************
    ADO.Net Core Reference
    Inside C#
    					

???

???? ID: 317069 - ????? ???????: 04 ?????? 2010 - ??????: 5.0
???? ???? ???? ??:
  • Microsoft .NET Framework 1.1
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
  • Microsoft .NET Framework 1.0
??????: 
kbhowtomaster kbmt KB317069 KbMthi
???? ?????? ????????
??????????: ?? ???? ?? ???? ??????? ?? ????? ?? Microsoft ????-?????? ?????????? ?????? ?????? ???? ??? ??. Microsoft ???? ??? ????-???????? ?? ????-???????? ????? ?????? ?? ???? ???????? ???? ?? ???? ????? ????? ??? ?? ??? ?????? ?? ???? ???? ???? ??? ????? ??. ???????, ????-???????? ???? ????? ???? ???? ???? ???. ?????, ????????, ?????-???? ?? ??????? ?? ???????? ?? ???? ???, ???? ?? ??? ?????? ???? ???? ??? ????? ??? ?? ???? ??. Microsoft ??????? ??? ???? ?? ?????? ?? ??????????, ????????? ?? ??? ?????? ?? ???? ????? ?? ???? ???????? ?? ??? ???? ????? ?? ??? ????????? ???? ??. Microsoft ????-?????? ?????????? ?? ????? ?????? ?? ?? ??? ??.
?????????? ?? ??????? ????????? ??????? ??:317069

??????????? ???

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com