Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Order of items read from a XDocument, by LINQ, Guaranteed?

Tags:

.net

xml

linq

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, and 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.

like image 725
msarchet Avatar asked Jul 20 '10 14:07

msarchet


1 Answers

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

An IEnumerable<T> of XElement containing the children of the XContainer that have a matching XName, in document order.

like image 59
Jon Skeet Avatar answered Oct 12 '22 02:10

Jon Skeet