private void SaveDocument(XDocument document)
{
// Sort the elements by package id and only take valid entries (one with both id and version)
var packageElements = (from e in document.Root.Elements("package")
let id = e.GetOptionalAttributeValue("id")
let version = e.GetOptionalAttributeValue("version")
where !String.IsNullOrEmpty(id) && !String.IsNullOrEmpty(version)
orderby id
select e).ToList();
// Remove all elements
document.Root.RemoveAll();
// Re-add them sorted
document.Root.Add(packageElements);
FileSystem.AddFile(_path, document.Save);
}