HOW TO: ????? ????????? XPath ???????? ???? System.Xml.XPath

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

?? ??? ??????

??????

???? ??? ??????? ???? ????? ??? ????? ??????? ?????? ?? ????? ????? System.Xml.XPath ????? ????????? XPath ?? ??????? .NET.

??????? System.Xml.XPath Namespace

????? ??? ????? ????? System.Xml.XPath ???? XPath ????? ??????? ?? .NET Framework. ????? ????? ????? System.Xml.XPath ????? ??????? 1.0 ???? XML Path (XPath) "????? ???? ??? ????????" (W3C). ????? ?? ????????? ??? ?????? XPath ?? ?????? ???? ??? W3C ??????:
XML Path Language (XPath) Version 1.0
????? ????? System.Xml.XPath ????? ??? ?????? ???????? ??????? ???? ?? ??????? ????? ????????? XPath ??????? resultsets ???? ?? ?????? ?????? ??????:
  • XPathDocument: ????? ???? ???? - ????? ??????? ??????? ??? ??????? ??????? XML ? ??????. ?? ????? XPathDocument ???????? ????? ??? ????? ??????? ??????? (XSLT) ? ???? ???????? XPath. ?????? ???? ???? ??????? (DOM) XPathDocument - ??????? ??? ???? ??????? (????? XML ?????? ?? ???? ???? ?? ???????) ???? ?? ???? ?????? ???????? ???????? ????? ?? ????? ?? ??? ????? ?? ????? ??? ??????? ??????? (XML) ?? ???.
  • XPathNavigator: ??? ????? ???????? ??? ???? ???????? XPath. ???? XPathNavigator ?????? ???????? ?????? ?? ????? ????????? XPath ????????? ?? ?????? XML ???? ?? ?????? ?? XPathDocument. ??? ????? ???? XPathNavigator ?????? ??????? ??????? CreateNavigator ?????? XPathDocument. ????? ????? ??????? XPathNavigator ??? ????? ??????? ????????? XPath executed ???? ????? ?????? ???? System.Xml.XPath.XPathExpression ?????? ????????? ????????.
  • XPathNodeIterator: ???? ??????- ??? ? ??????? - ??? ???????. ??? ??????? ????? ????? ?????? XPathNavigator ?????? ??????? XPath ??? ????? ???? XPathNodeIterator. ????? ??????? XPathNodeIterator ?????? resultset ???? ?? ?????? ?????? ??????? XPath.
  • XPathExpression: ?????? ????? ??????? XPath ??????. ??? ????? ????? ??????? XPath ??? ????? ????? ???????. ????? ????? XPath ?????? ??? ???? ?????? ???? ??? ?????? ?? ?????? ?? ??????? ??? ????????? XPath W3C. ????? ??????? ??????? ????? ?????? XPathNavigator ??? XPathExpression. ???? ??????? ?????? XPathExpression ?????? precompiled XPath ????? ??????? ?? ??????? ????? ????? ?????? XPathNavigator.
  • XPathException: ??????? .NET Framework ???? ?? ?????? ??? ???? ??? ????? ?????? ????? ??????? XPath. ????? ????? ?????? ????? ??? XPathNavigator ???? ??? XPathException.

????? ??????? XML ?????

?????? ??????? 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>

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

  1. ??? ????? Visual Studio .NET ?? ?? ?????? ????? Visual Basic ????? Windows .NET ????.
  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) ? ?????? ?? reuses ?????? ??? ????? ??????? XPath ??? ????? (????? DotNetBooks.xml).

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

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

???????

???? ???????: 317069 - ????? ??? ??????: 07/???/1426 - ??????: 4.2
????? ???
  • Microsoft .NET Framework 1.1
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
  • Microsoft .NET Framework 1.0
????? ??????: 
kbmt kbhowtomaster KB317069 KbMtar
????? ????
???: ??? ????? ??? ?????? ???????? ?????? ????? ???? ????? ?????????? ????? ?? ????????? ?????? ????. ???? ???? ?????????? ???? ?? ???????? ???????? ?????? ????????? ????? ????????? ???????? ????? ???????? ?????? ?? ?????? ??? ?? ???????? ???????? ?? ????? ??????? ?????? ??? ??????? ?????? ??. ?????? ?? ???? ??? ??????? ???????? ????? ?? ???? ????? ?????? ??? ????? ??? ????? ??????? ?? ????? ?? ?????? ??? ??? ??????? ??????? ?? ????? ????? ????? ????? ?????. ?? ????? ???? ?????????? ??????? ??? ????? ?? ??????? ?? ????? ?????? ?? ??? ????? ?? ????? ??????? ?? ???????? ?? ??? ???????. ???? ???? ?????????? ???????? ??? ????? ?????? ??????? ??????
???? ??? ????? ??????? ?????? ??????????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