XML ????? C# ?? ????? ?? ???? XPath ?????????? ?? ??? ?????? ???? ????

???? ?????? ???? ??????
???? ID: 308333 - ?? ???????? ?? ?????? ??? ?? ?? ???? ???? ???? ??.
??? ?? ??????? ???? | ??? ?? ??????? ????

?? ????? ??

??????

????????? ???? ?? ?? ???? ?? ?????? ???? ??XPathDocument???????? ???? XML ?? (XPath) ???? ?????????? ????? ??XPathNavigator????? XPath ????????????? ?? ????????? ?? ???? ????????? ??? ??????? ????? ?? ??? ???? ?? ??? ????????? ?????? ????? ???? ???? ???

??????????

????? ???? outlines ???????? ?????????, ??????????, ??????? ??????, ?? ???? ???? ???:
  • ????? C#
?? ???? ????? ?? ?? ?? ????? ???? ?? ?????? ???:
  • XML ??????
  • ??? ??? ?? ?? ???? XML ???? ?? ????
  • XPath ????????

??? XPath ?????????? ?? ??? XML ?????? ???? ????

  1. ??? ??? ????? C# ????? ????????? Visual Studio ??? ??? ???

    ???:: ?????? ?? Books.xml ??? ?? ??? ????? ?? ????? ???? ??? ?? ???? Books.xml ????? ??? ???? ???, ?? ????? ?? ?? .NET ?????????? ????????? ??? (SDK) Quickstarts ?? ??? ????? ?? ?? ????? ?? ???? ???? "??? ?? Quickstarts ??????? ???? ?? ?? ?????? ??????? ???? ?? ??? ???? ?????, ??????" ??? ????? Books.xml ?? ??? ????? ?? ??????? ????? ??? ???? ??? Quickstarts ??????? ??, ?? ????? ????? ??????? ??? ????? ?? ???? ??:
    ????????? Files\Microsoft.NET\FrameworkSDK\Samples\Quickstart\Howto\Samples\Xml\Transformxml\VB
    ?? ????? ?? ????????? \Bin\Debug ???????, ?????? ?? ????? ?? ????????? ??????? ?? ??????? ????? ?? ?? ???? ?? ??? ?????? ???
  2. ????????? ???? ?? ????????? ?? ?????? ???System.Xml??? ????? ???
  3. ??????? ????? ?????? ???????? ???XML, ??XPath???????? ???? ?? ???? ??? ??? ??? ??? ?? ???????? ??? declarations ????? ????? ?? ??? ?????? ???? ???? ?? ????? ?????? ????? ??????? ???? ?? ???? ??? ?? ???? declarations, follows ?? ??? ???:
    using System.Xml;
    using System.Xml.XPath;
    					
  4. ??????? ?? ????? ????? ????? ??XPathDocumentXML ???????? ?? ???? ?? ??? ???????? ????XpathNavigatorXPath ?????????????, ?????????? ???? ?? ??? ???????? ?? ??XPathNodeIteratorobject to iterate through selected nodes. Declare a????????object to hold the XPath expressions. Add the declaration code in the?????function in Class1.
       XPathNavigator nav;
       XPathDocument docNav;
       XPathNodeIterator NodeIter;
       String strExpression;
    					
  5. Load anXPathDocumentwith the sample file Books.xml. TheXPathDocumentclass uses Extensible Stylesheet Language Transformations (XSLT) to provide a fast and performance-oriented cache for XML document processing. It is similar to the XML Document Object Model (DOM) but is highly optimized for XSLT processing and the XPath data model.
        // Open the XML.
        docNav = new XPathDocument(@"c:\books.xml");
    					
  6. Create anXPathNavigatorfrom the document. TheXPathNavigatorobject is used for read-only XPath queries. The XPath queries may return a resulting value or many nodes.
         // Create a navigator to query with XPath.
         nav = docNav.CreateNavigator();
    					
  7. Create an XPath expression to find the average cost of a book. This XPath expression returns a single value. For full details on XPath syntax, see "XPath Syntax" in the "References" section.
         // Find the average cost of a book.
         // This expression uses standard XPath syntax.
         strExpression = "sum(/bookstore/book/price) div count(/bookstore/book/price)";
    					
  8. ?????Evaluate?? ??? ????XPathNavigatorobject to evaluate the XPath expression. TheEvaluatemethod returns the results of the expression.
        // Use the Evaluate method to return the evaluated expression.
        Console.WriteLine("The average cost of the books are {0}", nav.Evaluate(strExpression));
  9. Create an XPath expression to find all of the books that cost more than ten dollars. This XPath expression returns only Title nodes from the XML source.
        // Find the title of the books that are greater then $10.00.
        strExpression = "/bookstore/book/title[../price>10.00]";
    					
  10. Create anXPathNodeIteratorfor the nodes that are selected with the??? ?????? ??? ????XPathNavigator. TheXPathNodeIteratorrepresents an XPath nodeset and therefore supports operations on this nodeset.
        // Select the node and place the results in an iterator.
        NodeIter = nav.Select(strExpression);
  11. ?????XPathNodeIterator, which was returned from the??? ?????? ??? ????XPathNavigator, to move through the selected nodes. In this case, you can use theMoveNext?? ??? ????XPathNodeIteratorto iterate through all of the selected nodes.
      Console.WriteLine("List of expensive books:");
      //Iterate through the results showing the element value.
      while (NodeIter.MoveNext())
      {
          Console.WriteLine("Book Title: {0}", NodeIter.Current.Value);
      };
    					
  12. ?????ReadLinemethod to add a pause at the end of the console display to more readily display the above results.
        // Pause
        Console.ReadLine();
    					
  13. Build and run your project. Note that the results are displayed in the console window.

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

?? ?? ??????? ??? ??, ?? ?? ????? ????? ?????? ????? ??????? ?? ???? ??:
'System.Xml.XmlException' ?????? ?? ??? ????? unhandled System.xml.dll ??? ? ?? ??

???????? ???????: ?????? ???????
????? ?????? ??? ?? ????? ?????? ?? ???? ??:
docNav = new XPathDocument("c:\\books.xml");
				
??? ?????? ????????? ??????? ?? ????? ?????? ?? ???? ??? ?????? ?? ???, ????????? ???? ???? ??????? ????????? ????????? ?? ???? ??? ??? ?????? ????????? ??????? ?? ?? ?????? ?????????? ??:
<?xml version='1.0' ?>
????? ?? ?? ???? ?? ??? ????? ??? ?? ?? ?? ?????:
  • ?????? ????????? ??????? ??? ????? ??? ????? ????????? ??????? ?? ?? ?????? ?????????? ??:
    <?xml version='1.0'?>
    -??-
  • Remove the XML processing instruction from the Books.xml file.

??????

????? ????? Microsoft ??????? ?????? ?? ??????? ?? ??? ?????? ??::
?? ??? ?? ??????? ?????? ??? ?? ??????? ????
??????? ????
Books.xml ?? ??????? ????
???? ??????? ?? ???, ????? Microsoft ??? ?????? ?? ????::
XML in .NET: .NET Framework XML Classes and C# Offer Simple, Scalable Data Manipulation
HTTP://MSDN.Microsoft.com/msdnmag/issues/01/01/XML/default.aspx

XPathNavigator Class
http://msdn.microsoft.com/en-us/library/system.xml.xpath.xpathnavigator.aspx

XPathDocument Class
http://msdn.microsoft.com/en-us/library/system.xml.xpath.xpathdocument.aspx

XPathNodeIterator Class
http://msdn.microsoft.com/en-us/library/system.xml.xpath.xpathnodeiterator.aspx

XSLT Transformations with the XslTransform Class
http://msdn.microsoft.com/en-us/library/47yh29a9.aspx

XPath Syntax
http://msdn2.microsoft.com/en-us/library/ms763742.aspx
For more information about XPath, visit the following World Wide Web Consortium (W3C) Web site:
XML Path Language (XPath)
Version 1.0: W3C Recommendation 16 November 1999
http://www.w3.org/TR/1999/REC-xpath-19991116

???

???? ID: 308333 - ????? ???????: 04 ?????? 2010 - ??????: 2.0
???? ???? ???? ??:
  • Microsoft Visual C# 2008 Express Edition
  • Microsoft Visual C# .NET 2002 Standard Edition
??????: 
kbdownload kbhowtomaster kbmt KB308333 KbMthi
???? ?????? ????????
??????????: ?? ???? ?? ???? ??????? ?? ????? ?? Microsoft ????-?????? ?????????? ?????? ?????? ???? ??? ??. Microsoft ???? ??? ????-???????? ?? ????-???????? ????? ?????? ?? ???? ???????? ???? ?? ???? ????? ????? ??? ?? ??? ?????? ?? ???? ???? ???? ??? ????? ??. ???????, ????-???????? ???? ????? ???? ???? ???? ???. ?????, ????????, ?????-???? ?? ??????? ?? ???????? ?? ???? ???, ???? ?? ??? ?????? ???? ???? ??? ????? ??? ?? ???? ??. Microsoft ??????? ??? ???? ?? ?????? ?? ??????????, ????????? ?? ??? ?????? ?? ???? ????? ?? ???? ???????? ?? ??? ???? ????? ?? ??? ????????? ???? ??. Microsoft ????-?????? ?????????? ?? ????? ?????? ?? ?? ??? ??.
?????????? ?? ??????? ????????? ??????? ??:308333

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

 

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