Is Order of items read from a XDocument, by LINQ, Guaranteed?
So what I'm doing is using an xml document to determine the order in which certain SQL scripts need to be ran by a database update.
The XML follows this format
<ScriptRules>
<Database databaseName = “string”>
<Folder folderPath = “string” executeAllFiles = boolean>
<file order=”first or last”>“Filename”</file>
</Folder>
</Database>
</ScriptRules>
So the class that executes all of the sql
files on the database looks at this at changes it's connection and executes files from folders depending on what the config file has told it to do.
Now my question is this:
Let's say that I have a Document that has 4 Database
nodes, a开发者_运维技巧nd each of them have n
number of Folder
nodes inside with an assortment of file
nodes inside of that.
Is it logical of me to assume that if inside of a For Each
loop over the Database
nodes that I have retrieved into a XElement
using database.Elements("Database")
that they will be pulled in the order that they appear in the xml file? (same for the file nodes)
As far as I have been able to tell this is the case but I just wanted to verify before I start using this on production databases.
Yes, Elements
returns the elements in document order. From the docs, in the "return value":
An
IEnumerable<T>
ofXElement
containing the children of theXContainer
that have a matchingXName
, in document order.
Yes, so long as you don't use PLINQ, and even then you can ask it to give 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
精彩评论