XmlDocument SelectNodes (Xpath): Result Order

This is an example of XML from MSDN.

<?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>
  </book>
  <book genre="novel" publicationdate="1992" bk:ISBN="1-861002-30-1">
    <title>The Handmaid Tale</title>
  </book>
  <book genre="novel" publicationdate="1991" bk:ISBN="1-861001-57-6">
    <title>Emma</title>
  </book>
  <book genre="novel" publicationdate="1982" bk:ISBN="1-861001-45-3">
    <title>Sense and Sensibility</title>
  </book>
</bookstore>

When I select all nodes of a book using the following code, what order will these nodes have?

XmlDocument doc = new XmlDocument();
doc.Load("booksort.xml");

var nodeList =doc.SelectNodes("bookstore/book");

Will the order of elements in the nodelist be the same as the order in xml? Is this order guaranteed?

+3
source share
2 answers

Yes. Looking at it in a reflector, this method ends up using XPathNodeIteratorone that is documented for iteration in document order. http://msdn.microsoft.com/en-us/library/1212yhbf.aspx

+6
source

XPathNavigator XmlDocument.Select *. XPathExpression AddSort.

+1

Source: https://habr.com/ru/post/1739127/


All Articles