1

I've got a DBML file in my project with all my LinqToSql objects. Initially I imported them from the DB, and all was well. Now as my DB has been growing, I've been adding the new tables to the diagram in the O/R Designer, but they always get appended to the end of the XML. This is a bit of a pain, because when I'm defining foreign keys, it always lists the available tables in the order in which they appear in the XML.

Any ideas how to sort the XML table declarations alphabetically according to the table name?

Gert Arnold
  • 105,341
  • 31
  • 202
  • 291
Shaul Behr
  • 36,951
  • 69
  • 249
  • 387

2 Answers2

1

I know this is old, but I also want to sort the tables and functions in my DBML to make it more manageable in Git. The following console application code seems to work pretty well. You can drag and drop a DBML file onto the exe, or you could set up a bat file or build event in your project(s).

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace DbmlSorter
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length == 0)
                return;

            var fileName = args[0];

            try
            {
                if (!File.Exists(fileName))
                    return;

                SortElements(fileName);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

            Console.WriteLine();
            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();
        }

        private static void SortElements(string fileName)
        {
            var root        = XElement.Load(fileName);

            var connections = new SortedDictionary<string, XElement>();
            var tables      = new SortedDictionary<string, XElement>();
            var functions   = new SortedDictionary<string, XElement>();
            var others      = new SortedDictionary<string, XElement>();

            foreach (var element in root.Elements())
            {
                var key = element.ToString();

                if (key.StartsWith("<Connection"))
                    connections.Add(key, element);

                else if (key.StartsWith("<Table"))
                    tables.Add(key, element);

                else if (key.StartsWith("<Function"))
                    functions.Add(key, element);

                else
                    others.Add(key, element);
            }

            root.RemoveNodes();

            foreach (var pair in connections)
            {
                root.Add(pair.Value);

                Console.WriteLine(pair.Key);
            }

            foreach (var pair in tables)
            {
                root.Add(pair.Value);

                Console.WriteLine(pair.Key);
            }

            foreach (var pair in functions)
            {
                root.Add(pair.Value);

                Console.WriteLine(pair.Key);
            }

            foreach (var pair in others)
            {
                root.Add(pair.Value);

                Console.WriteLine(pair.Key);
            }

            root.Save(fileName);
        }
    }
}
Michael Csikos
  • 688
  • 9
  • 11
1

A possible solution is to write a small application that reads in the XML, sorts it to your liking and outputs the updated version.

Pieter van Ginkel
  • 29,160
  • 8
  • 71
  • 111