How to quickly find information from an XML file

Source: Internet
Author: User
Tags implement net string version xmlns xpath xsl
XML in the network age, XML files play a role in the preservation and transmission of data. SOAP protocols communicate information through XML, databases are accessed through XML files, and so on. So how quickly do you  you need from an XML file?

As we know, Java JAXP and Microsoft.NET have XML parsers, Microsoft.NET is read-side analysis, and JAXP is read in memory and then analyzed (there is an event mechanism to read), in short, is not conducive to fast reading. Based on this, both Microsoft.NET and JAXP provide an XPath mechanism to quickly navigate to the desired nodes in the XML file.

For example, there is an XML file: Booksort.xml:

<?xml version= "1.0"?>

<!--a fragment of a book store inventory database-->

<bookstore xmlns:bk= "Urn:samples" >

<book genre= "novel" Publicationdate= "1997" bk:isbn= "1-861001-57-8" >

<title>pride and Prejudice</title>

<author>

<first-name>Jane</first-name>

<last-name>Austen</last-name>

</author>

<price>24.95</price>

</book>

<book genre= "novel" Publicationdate= "1992" bk:isbn= "1-861002-30-1" >

<title>the Handmaid ' s tale</title>

<author>

<first-name>Margaret</first-name>

<last-name>Atwood</last-name>

</author>

<price>29.95</price>

</book>

<book genre= "novel" Publicationdate= "1991" bk:isbn= "1-861001-57-6" >

<title>Emma</title>

<author>

<first-name>Jane</first-name>

<last-name>Austen</last-name>

</author>

<price>19.95</price>

</book>

<book genre= "novel" Publicationdate= "1982" bk:isbn= "1-861001-45-3" >

<title>sense and Sensibility</title>

<author>

<first-name>Jane</first-name>

<last-name>Austen</last-name>

</author>

<price>19.95</price>

</book>

</bookstore>

If we want to quickly find all the title names of "Last-name" equal to "Austen", you can get it in the following ways:

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 ("title", Xmlsortorder.ascending, Xmlcaseorder.none, "", Xmldatatype.text);


XPathNodeIterator iterator = nav. Select (expr);

while (iterator. MoveNext ())

{

XPathNavigator nav2 = iterator. 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 that the lookup is correct.

Using some of the features in XPath, you can also implement simple sorting and simple operations. If data is often summarized in a database, XPath can be used to implement it.

Such as:

Order.xml

<!--represents a customer order-->

<order>

<book isbn= ' 10-861003-324 ' >

<title>the Handmaid ' s tale</title>

<price>19.95</price>

</book>

&LT;CD isbn= ' 2-3631-4 ' >

<title>Americana</title>

<price>16.95</price>

</cd>

</order>


And: books.xml

<?xml version= "1.0"?>

<!--This file represents a fragment of the book store inventory database-->

<bookstore>

<book cc= "dd" xmlns:bk= "Urn:sample" xmlns:ns= "http://www". Any.com "Genre=" Autobiography "Publicationdate=" 1981 "isbn=" 1-861003-11-0 ">

<title>the Autobiography of Benjamin franklin</title>

<ns:author>

<first-name>Benjamin</first-name>

<last-name>Franklin</last-name>

</ns:author>

<price>8.99</price>

</book>

<book genre= "novel" Publicationdate= "1967" isbn= "0-201-63361-2" >

<title>the confidence man</title>

<author>

<first-name>Herman</first-name>

<last-name>Melville</last-name>

</author>

<price>11.99</price>

</book>

<book genre= "Philosophy" Publicationdate= "1991" isbn= "1-861001-57-6" >

<title>the gorgias</title>

<author>

<name>Plato</name>

</author>

<price>9.99</price>

</book>

</bookstore>


We can sum the price in the XML file to get the total number of prices.

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 Evaluatesample ();

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);


XPathNodeIterator myxpathnodeiterator = 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 results are as follows:

30.97

36.9

36.9

We can see that 30.97 is the sum of all the price values in the Books.xml, and 36.9 is the sum of all the price values in the Order.xml. Xpah not only can quickly find information, but also can do some basic processing of information.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.