Search This Blog

Monday, August 15, 2011

Convert XElements to Json

You need to capture the data type in a class that is serializable.
In this example I use Employee to do so. The first action will be to construct an IEnumerable of Employee:


XElement empXml = GetEmployees();
 var empJson = from emp in empXml.Descendants("Employee")
               select new Employee
               {
                      ID = emp.Element("ID").Value,
                      FirstName = emp.Element("FirstName").Value,
                      Department = emp.Element("Department").Value,
                      City = emp.Element("City").Value
               };

The second step is to serialize the enumeration into the memory stream.
var ser = new  DataContractJsonSerializer(typeof(IEnumerable));
  var ms = new MemoryStream();
  ser.WriteObject(ms, empJson);
And at last, we can take the generated stream as a string just before closing the stream.

string json = Encoding.Default.GetString(ms.ToArray());
ms.Close();

Animate Image Left To Right

The basics of any animation is moving something from one location to the other. In this code snippet I use to Image elements to slide away previously selected image and slide in the newly selected one.


The Image elements need to sit in a Canvas in order to let the Left property make sence.


assuming that I have already given the previously selected image, my new image file will be given through a function call like
AnimateLeftToRight(selectedFile);

This method will swap the images and create a storyboard to begin animating. I start the newImage from x-position that equals to minus Window.Width and the old one starts from 0 and moves to Window.Width:


/// 
/// Begins a story which animates two images from left to right/// the old image takes the source from te new image just before assigning the new one/// and they move together in the distance of Window.Width/// 
/// the path of the new image
private void AnimateLeftToRight(string path)
{
    oldImage.Source = newImage.Source;
    newImage.Source = new BitmapImage(new Uri(path));

    var myStoryboard = new Storyboard();

    myStoryboard.Children.Add(MakeAnimation(-this.Width, 0, "newImage"));
    myStoryboard.Children.Add(MakeAnimation(0, this.Width, "oldImage"));

    myStoryboard.Begin(this);
}


Making animation can be done in different ways using different EasingFunction, But this one looked easy to understand and look nice on my example:


/// 
/// Creates a DoubleAnimation that moves the target from left to right/// 
/// starting point X
/// ending point X
/// the target element to set the x to 
/// the created animation
private DoubleAnimation MakeAnimation(double from, double to, string target)
{
    const double durationSeconds = 1;

    var animation = new DoubleAnimation
                        {
                            Duration = new Duration(TimeSpan.FromSeconds(durationSeconds)),
                            AutoReverse = false,
                            From = from,
                            To = to,
                            EasingFunction = new PowerEase()
                                                 {
                                                     EasingMode = EasingMode.EaseInOut,
                                                     Power = 5
                                                 }
                        };

    Storyboard.SetTargetName(animation, target);
    Storyboard.SetTargetProperty(animation, new PropertyPath(Canvas.LeftProperty));

    return animation;
}

Linq: Group by and sort

The following LINQ statement was the first one I wrote:

var hsQ = from hspt in hospitals
          orderby hspt.County, hspt.City
          group hspt by hspt.County;

It obviously groups by country and then sorts by city.


However, I found the following even better as it groups first by country, sorts by country and then sorts within the group by city:

var hsQ = from hspt in hospitals
          orderby hspt.City
          group hspt by hspt.County into hsptGroup
          orderby hsptGroup.First().County
          select hsptGroup;