Transform XML Strings

This one is a quickie. It applies XML transformations where the source and destination are both strings.

string xml = txtXml.Text;
string xslString = txtTranstring.Text;
string transformedXml = string.Empty;

XslCompiledTransform xsl = new XslCompiledTransform();
xsl.Load(new XmlTextReader(new StringReader(xslString)));
using (var stringWriter = new StringWriter())
    using (var xmlWriter = XmlWriter.Create(stringWriter))
        xsl.Transform(new XmlTextReader(new StringReader(xml)), xmlWriter);

    transformedXml = stringWriter.ToString();

txtResult.Text = transformedXml;

OrderBy Extension for Sorting by Multiple Column Names

I often use the OrderBy extension ( to sort results using Linq method syntax. However as of late I have found it necessary to sort by multiple fields by using the field names. While it hasn’t been something critical I have been seeking a way to do this. Today I was able to make my own extension that can take the following types of parameters.

IEnumerable<Employees> sortedResult = employees.OrderBy("department, division asc, lastname desc");

If you try it out and find any issues let me know so I can update the code. While this works on IEnumerable (as it is what I was working with) I will work on an IQueriable version.

using System;
using System.Collections.Generic;
using System.Linq;

namespace MyProject
    public static class Extensions
        private enum OrderByType : Int16
            Asc = 0,
            Desc = 1

        public static IEnumerable<T> OrderBy<T>(this IEnumerable<T> enumerable, string sortString)
            if (string.IsNullOrWhiteSpace(sortString))
                throw new MissingMemberException("Sortstring missing or empty.");

            T obj = enumerable.FirstOrDefault();
            string[] orderBy, orderBys = sortString.Split(',');
            string fieldname = string.Empty;
            OrderByType direction;

            IOrderedEnumerable<T> result = null;
            for (int i = 0; i < orderBys.Length; i++)
                if (string.IsNullOrWhiteSpace(orderBys[i]))
                    throw new MissingMemberException("Invalid sortstring.");

                orderBy = orderBys[i].Split(' ').Where(s => !string.IsNullOrWhiteSpace(s)).ToArray(); // 0 = fieldname, 1 = direction
                if (1 <= orderBy.Length) fieldname = orderBy[0];
                Enum.TryParse((2 == orderBy.Length ? orderBy[1] : string.Empty), true, out direction);

                var propertyInfo = obj.GetType().GetProperty(fieldname, System.Reflection.BindingFlags.IgnoreCase   |
                                                                        System.Reflection.BindingFlags.Public       |

                if (0 < i)
                    if (direction.Equals(OrderByType.Asc))
                        result = result.ThenBy(f => propertyInfo.GetValue(f, null));
                    else if (direction.Equals(OrderByType.Desc))
                        result = result.ThenByDescending(f => propertyInfo.GetValue(f, null));
                else if (0 == i)
                    if (direction.Equals(OrderByType.Asc))
                        result = enumerable.OrderBy(f => propertyInfo.GetValue(f, null));
                    else if (direction.Equals(OrderByType.Desc))
                        result = enumerable.OrderByDescending(f => propertyInfo.GetValue(f, null));

            return result;