Extension method for sorting keys in RESX file

using System.Linq;
using System.Xml.Linq;
using System.Xml;
using System;

namespace SortResxFileKeys
{
  public static class XDocumentExtensionMethods
  {
    public static XDocument SortXDocumentResourceKeys(this XDocument xDoc)
    {
      XElement root = xDoc.Root;
      return new XDocument(
        new XElement(root.Name,
          root.Nodes().Where(n => n.NodeType == XmlNodeType.Comment),
          root.Elements().Where(n => n.Name.LocalName == “schema”),
          root.Elements(“resheader”).OrderBy(names),
          root.Elements(“assembly”).OrderBy(names),
          root.Elements(“metadata”).OrderBy(names),
          root.Elements(“data”).OrderBy(names)));
    }

    static Func<XElement, string> names =
            n => n.Attribute(“name”).ToString();
  }
}

Afterwards, the usage is simple

    XDocument xDoc = XDocument.Load(“SomeResxFileHere.resx”);
    xDoc.SortXDocumentResourceKeys();

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: