Extension method for sorting keys in RESX file

2013/09/12

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

%d bloggers like this: