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();


Searching files with LINQ

2009/11/10

LINQ is very powerful in many aspects of programming; for example, you can simulate the search for files feature of some known applications (Windows Explorer or Total Commander) with very few lines of code, as shown below:

    public List<FileInfo> SearchForFiles(String path, String ext, String search)
    {
        DirectoryInfo folder = new DirectoryInfo(path);
        return (from file in folder.GetFiles(ext, SearchOption.AllDirectories)
                where file.Name.ToLower().Contains(search.ToLower())
                orderby file.Name, file.Extension
                select file).ToList();
    }

Then, you can use in several ways the resulted list, for example by adding to your Windows Form application a DataGridView and setting its DataSource as following:

    dgvFiles.DataSource = SearchForFiles(path, extension, searchTerm).ToList();