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


WPF Application.DoEvents()

2010/09/25

VB6 or even .Net programmers know that in order not to block a desktop application UI when processing longer tasks, there is a little trick that can be applied, if the tasks contain a loop, which loop will allow a call to:
Application.DoEvents()
which will process Windows waiting messages.
But in WPF there is no such default method.
There are different approaches and implementations, but if you look for a fast solution, you can just add a reference to
System.Windows.Forms.dll
and the corresponding using directive and Application.DoEvents() is ready to use !
I usually use a directive with alias, in order not to conflict with other existing namespaces:
using SysWinForms = System.Windows.Forms;
So you can simply call:
SysWinForms.Application.DoEvents();


Using Reflection for retrieving values of static properties

2010/07/07

I had a list of classes for which I wanted to retrieve the static property Description; this is how I found out it can be done using Reflection:

Assembly assembly = Assembly.GetExecutingAssembly();
var types = assembly.GetTypes();
foreach (var type in types)
{
    if (!type.IsClass)
        continue;
    if (!type.IsSubclassOf(typeof(MyBaseClass)))
        continue;
    object obj = Activator.CreateInstance(type, new object[] { 0, 0 });
    PropertyInfo pi = obj.GetType().GetProperty("Description");
    string propValue = pi.GetValue(null, null).ToString();
}

Note:If Description is a property of the base class MyBaseClass, you’ll have to use obj.GetType().BaseType.GetProperty(“Description”); for this to work.
Also, as you can see, at least in my case, I need to create instances of the classes I work with, because Description property is defined in a base class, not in the derived ones, and the static property will be populated via constructors.


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

Download a file in C#

2009/10/26

Easy task !
Import the System.Net namespace and after that use the DownloadFile method of the WebClient class:

public void DownloadFile(string link, string destination)
{
    using (WebClient webClient = new WebClient())
    {
        webClient.DownloadFile(link, destination);
    }
}

Navigate with C# WebBrowser control to a link and wait for full page loading

2009/10/26

If you need to build an application that is using the functionality of the WebBrowser control and want to wait until a page is loaded (somehow simulating the synchronous loading of a page), you can use the following piece of code:

private const int sleepTimeMiliseconds = 5;

public void NavigateAndWaitForLoad(WebBrowser wb, string link, int waitTime)
{
    wb.Navigate(link);
    int count = 0;
    while (wb.ReadyState != WebBrowserReadyState.Complete)
    {
        Thread.Sleep(sleepTimeMiliseconds);
        Application.DoEvents();
        count++;
        if (count > waitTime / sleepTimeMiliseconds)
            break;
    }
}


First peek on retrieving Entity Framework entities with Lambda expressions

2009/10/14

After adding an ADO.NET Entity Data Model item to the .Net project, you can easily retrieve a list of entities using Lambda expressions. Let’s suppose that the data model added to the project is called MyDataEntities and that it links to a table called Person, having Name as column. The following code can be used to retrieve the list:

    public static List<Person> GetPersons()
    {
        try
        {
            using (MyDataEntities context = new MyDataEntities())
            {
                return context.Person.OrderBy(p => p.Name).ToList<Person>();
            }
        }
        catch
        {
            // Do exception handling here
            throw;
        }
    }

Write to text file in C# using TextWriter

2009/10/07

Very simple:

    public static void WriteLog(String logFileName, String msg)
    {
        try
        {
            TextWriter writer = new StreamWriter(logFileName, true);
            writer.WriteLine(DateTime.Now.ToString("mm:ss") + " " + msg);
            writer.Flush();
            writer.Close();
            writer = null;
        }
        catch
        {
        }
    }

The second parameter of the StreamWriter constructor specifies if the text will be appended to the file; if set to false, the file will be overwritten.


Internalization in Java using ResourceBundle

2009/09/29

In order to implement localization in your Java application, you can use ResourceBundle and PropertyResourceBundle classes, as following:

Step 1: Create a method that receives a string key for which you need the “translation”:

public String getI18NString(String key) {
    if (key == null)
        return "[" + key + "]";;
    
    try {
        PropertyResourceBundle resBundle = (PropertyResourceBundle) ResourceBundle.getBundle("localization");
        return resBundle.getString(key);
    } catch (Exception e) {
    }
    
    return "[" + key + "]";
}

Step 2: Add localization files to your project, under a source folder. The default translations will be grouped under a file named like localization.properties, the additional languages will use files like localization_fr_FR.properties or localization_de_DE.properties:

01InternalizationInJavaLocalizationFiles

The content of such a file will be as following:

BTN_OK=Approuver
BTN_CANCEL=Annuler

Step 3: Test the implemented method:

@Test
public void testI18N() {
    assertEquals(getI18NString("BTN_CANCEL"), "Cancel");
    Locale.setDefault(new Locale("ro", "RO"));
    assertEquals(getI18NString("BTN_CANCEL"), "Anuleaza");
    Locale.setDefault(new Locale("fr", "FR"));
    assertEquals(getI18NString("BTN_CANCEL"), "Annuler");
}

Create XSD file with JAXB

2009/09/26

This post is a sequel of the two previous ones talking about serializing Java classes to XML through JAXB in Eclipse. So, it started when I wanted to serialize objects without using an XSD schema… and the only solution I found was by creating the ObjectFactory.java class that JAXB needs when going through marshalling process (check More on XML serialization with JAXB post).

Still, this solution was not good enough for me, I wanted to find a mechanism inside JAXB to do the task, not outside it, or by cheating… So I googled some more and found a way to generate an XML schema (XSD file) from Java classes; not what I was looking for, but interesting (and tricky) stuff. In order to do this, add to your Java project (described in previous 2 articles) the following method:

public void generateSchema(String suggestedSchemaName) throws Exception
{
    final File parentFolder = new File(".");
    final String schemaName = suggestedSchemaName;
        
    JAXBContext jc = JAXBContext.newInstance(Graph.class);
    jc.generateSchema(new SchemaOutputResolver() {
        @Override
        public Result createOutput(String namespace, String schema) throws IOException {
            return new StreamResult(new File(parentFolder, schemaName));
        }
    });
}

As you can see, JAXBContext class has the generateSchema method, which takes an instance of a SchemaOutputResolver class, that needs to override the createOutput method in order to output the XSD file. So you can afterwards call the method presented above as follow:

generateSchema(“graphXmlSchema.xsd”);