Is the order of items read from XDocument, by LINQ, Guaranteed?

So what I'm doing is using an XML document to determine the order in which some SQL scripts should run when updating the database.

XML follows this format

<ScriptRules> <Database databaseName = "string"> <Folder folderPath = "string" executeAllFiles = boolean> <file order="first or last">"Filename"</file> </Folder> </Database> </ScriptRules> 

Thus, a class that executes all sql files in the database looks at this when its connection changes and executes files from folders depending on what the configuration file told it to.

Now my question is this:

Let's say that I have a document with four Database nodes, and each of them has n number of Folder nodes inside with a set of file nodes inside this.

Is it possible to assume that if inside the For Each loop over the Database nodes that I received in XElement using database.Elements("Database") , they will be pulled in the order in which they appear in the xml file? (same for file nodes)

As far as I could tell, this is true, but I just wanted to check before starting to use this in production databases.

+7
xml linq
source share
2 answers

Yes, Elements returns elements in the order of the document. From the docs in the "return value":

An IEnumerable<T> of XElement containing XContainer children that have an XName match in document order.

+9
source share

Yes, if you do not use PLINQ, and even then you can ask him to provide you the elements in order.

  var orderedCities = (from city in cities.AsParallel().AsOrdered() where city.Population > 10000 select city) .Take(1000); 

http://msdn.microsoft.com/en-us/library/dd460677.aspx

+3
source share

All Articles