In the network era, the XML file has played a role of saving and transferring data. The SOAP protocol communicates information through XML, the database passes XML file access, and more. So how do you get the required information from an XML file?
We know that there is an XML analyzer in Java's JAXP and Microsoft.net, and Microsoft.net is a side-read analysis, while JAXP is read in memory and then analyzes (there is an event mechanism to read), in all, in all, It is not conducive to quick reading. Based on this, all Microsoft.Net and JAXP provide XPath mechanism to quickly locate the nodes required in the XML file.
For example, there is an XML file: booksort.xml:
XML Version = "1.0"?>
author>
book>
author>
book>
author>
book>
author>
book>
bookstore>
If we want to quickly find all the title names equal to "AUSTEN", you can get:
XmlReadersample.cs
//Corelib.Net/system.xml.xsl/xpathdocument class
// Author: Any
Using system;
Using system.io;
USING SYSTEM.XML;
Using system.xml.xpath;
Public Class XmlReadersample
{
Public static void main ()
{
XmlTextReader myXTReader = New XMLTextReader ("Booksort.xml");
XmlReader myxreader = myxtreader;
XpathDocument Doc = New XpathDocument (MyXReader);
Xpathnavigator NAV = doc.createnavigator ();
XPATHEXPRESSION EXPR;
Expr = Nav.Compile ("Descendant :: Book [Author / Last-Name = 'Austen');
//expr.addsort ("teple ", xmlsortorder.ascending, xmlcaseorder.none," ", xmldattype.text);
XpathnodeEiterator itrator = nav.select (expr);
While (item.movenext ())
{
Xpathnavigator nav2 = item.current;
Nav2.movetofirstchild ();
Console.writeline ("Book Title: {0}", Nav2.Value);
}
}
}
Run this program, the result is:
Book Title: Pride and Prejudice
Book Title: Emma
Book Title: Sense and Sensibility
You can see the looks correct.
Simple sorting and simple operations can also be achieved using some of the features in XPath. If you want to summarize the data in the database, you can implement Xpath.
Such as:
ORDER.XML
book>
cd>
order>
And: books.xml
XML Version = "1.0"?>
author>
book>
author>
book>
bookstore>
We can sum up the price in the XML file to get the total price.
Evaluate.cs
//Corelib.Net/system.xml.xsl/xpathnavigator class
// Author: Any
Using system;
Using system.io;
USING SYSTEM.XML;
Using system.xml.xpath;
Public Class Evaluatesample
{
Public static void main ()
{
EvaluateSample Myevaluatesample = new evataSample ();
myevaluateSample.test ("Books.xml");
}
Public Void Test (String Args)
{
Try
{
// Test Evaluate (String);
XpathDocument MyXPathDocument = New XPathDocument (args);
Xpathnavigator myxpathnavigator = myxpathdocument.createnavigator ();
Console.writeline (MyXPathnavigator.evaluate ("Sum (Descendant :: Book / Price)))
// Testevaluate (XPATHEXPRESSION); xmlDocument doc = new xmldocument ();
Doc.Load ("Order.xml");
Xpathnavigator NAV = doc.createnavigator ();
XPATHEXPRESSION EXPR = Nav.Compile ("SUM (// Price / text ()")
Console.writeline (Nav.Evaluate (expr));
// Testevaluate (XPATHEXPRESSION);
XpathnodeEiterator = nav.select ("Descendant :: Book / Title);
Expr = nav.compile ("SUM (// price / text ()")
Console.writeline (Nav.Evaluate (expr, myxpathnodeiterator);
}
Catch (Exception E)
{
Console.writeline ("Exception: {0}", E.TOSTRING ());
}
}
}
Run this program, the result is as follows:
30.97
36.9
36.9
We can see that 30.97 is the sum of all the Price values in Books.xml, and 36.9 is the sum of all equal values in ORDER.XML. The information can not only quickly look for information through XPAH, but also some basic processing.